update catch在catch异常中获取错误,消息在描述中

时间:2013-10-16 10:37:51

标签: c# asp.net sql-server

我不明白我得到这种错误......下面你可以看到更新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();
            }
        }
}

当我调试代码时...它会进入catch并显示错误消息:'。'附近的语法不正确。任何人都可以知道我错在哪里......如果有人的话会很棒更正我的代码,这将更新数据库中的表。

非常感谢。

4 个答案:

答案 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);

答案 1 :(得分:1)

好吧,首先upadate不是SQL命令。

现在您似乎对SQL命令参数的使用有了一些了解。那么为什么你们放弃了真正的正义之路并在最后连接了一个参数呢?!

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

答案 2 :(得分: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);

答案 3 :(得分:0)

 using (SqlConnection con = new SqlConnection())
        {
            string title = "niraj";
            SqlCommand cmd = new SqlCommand("update [dbo].[PatientDetails] set title=@title", con);
            cmd.Parameters.AddWithValue("@title", title.ToString());
            cmd.ExecuteNonQuery();
            con.Close();
        }