2013-10-16 27 views
-2

我不明白我收到這種錯誤...下面你可以看到更新sqlquery。更新獲取錯誤在捕獲異常和消息是在說明

protected void btnupdate_Click(object sender, EventArgs e) 
{ 
    string pID = Convert.ToString(Session["PatientId"]); 
    if (!string.IsNullOrEmpty(pID)) 
    { 
     int patientID = Convert.ToInt32(pID); 

     SqlConnection connew = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); 



      SqlCommand cmd = new SqlCommand("Upadate [dbo].[PatientDetails] set [title] = @pttit, [sex] = @ptgen, [lastname] = @ptlastnm, " + 
        " [birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = @ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark " + 
                     "where [PatientId] = '"+pID+"'", connew); 



      cmd.Parameters.AddWithValue("@pttit", txtpttitle.Text); 
      cmd.Parameters.AddWithValue("@ptgen", txtgender.Text); 
      cmd.Parameters.AddWithValue("@ptlastnm", txtptlastnm.Text); 
      cmd.Parameters.AddWithValue("@ptbirth", txtptbirthday.Text); 
      cmd.Parameters.AddWithValue("@ptfirstnm", txtptfirstnm.Text); 
      cmd.Parameters.AddWithValue("@ptmiddlenm", txtptmiddlenm.Text); 
      cmd.Parameters.AddWithValue("@ptremarkln", txtptremarkline.Text); 
      cmd.Parameters.AddWithValue("@ptremark", txtremarks.Text); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = connew; 

      if (connew.State == ConnectionState.Closed) 
      { 
       connew.Open(); 
      } 
      try 
      {       
       //rowsaffected = cmd.ExecuteNonQuery(); 
       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Error Occured: " + ex.Message.ToString()); 
      } 
      finally 
      { 
       connew.Close(); 
       cmd.Dispose(); 
      } 
     } 
} 

當我調試的代碼......它進入捕捉並顯示錯誤消息:附近有語法錯誤.'....有誰能夠有地方我錯了....它會想法如果有人糾正我的代碼將更新數據庫中的表,那就太好了。

非常感謝。

+0

在這種情況下使用'存儲過程'不是更容易嗎? –

回答

2

你拼寫錯了更新......

SqlCommand cmd = new SqlCommand("Upadate [dbo].[PatientDetails] set [title] = @pttit, [sex] = @ptgen, [lastname] = @ptlastnm, " + 
       " [birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = @ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark " + 
                     "where [PatientId] = '"+pID+"'", connew); 
+0

有什麼區別?你能告訴我那個區別嗎? –

+1

更新!= Upadate –

+0

但你的答案有Upadate不更新! –

1

那麼,一開始upadate是不是一個SQL命令。

現在,您似乎對使用SQL命令參數有一些瞭解。那麼爲什麼你放棄了真正正義的道路,並最終連接了一個參數?!

where [PatientId] = '"+pID+"' 
+0

我不明白你說的是什麼....對不起 – Pratik

+0

@Pratik不要做'where [PatientId] ='「+ pID +」'',做'where [PatientID] = @ patientID' – podiluska

+0

thx ...我做到了...... – Pratik

0

試試這個代碼,在命令參數泰德添加使用pid在查詢中調用CMD PARM名

SqlCommand cmd = new SqlCommand(@"Update [dbo].[PatientDetails] set [title] = @pttit, [sex] = 

@ptgen, [lastname] = @ptlastnm,[birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = 

@ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark where [PatientId] = @pID", connew); 



      cmd.Parameters.AddWithValue("@pttit", txtpttitle.Text); 
      cmd.Parameters.AddWithValue("@ptgen", txtgender.Text); 
      cmd.Parameters.AddWithValue("@ptlastnm", txtptlastnm.Text); 
      cmd.Parameters.AddWithValue("@ptbirth", txtptbirthday.Text); 
      cmd.Parameters.AddWithValue("@ptfirstnm", txtptfirstnm.Text); 
      cmd.Parameters.AddWithValue("@ptmiddlenm", txtptmiddlenm.Text); 
      cmd.Parameters.AddWithValue("@ptremarkln", txtptremarkline.Text); 
      cmd.Parameters.AddWithValue("@ptremark", txtremarks.Text); 
      cmd.Parameters.AddWithValue("@pID",pID); 
0
using (SqlConnection con = new SqlConnection()) 
     { 
      string title = "niraj"; 
      SqlCommand cmd = new SqlCommand("update [dbo].[PatientDetails] set [email protected]", con); 
      cmd.Parameters.AddWithValue("@title", title.ToString()); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
     } 
+0

我希望它會工作。但給參數像上面的代碼。 –

+0

OP已經做到了! –

相關問題