如何修复SQL语法错误...最多...在第1行的'())'附近使用正确的语法

时间:2019-05-04 02:47:19

标签: c# mysql asp.net

我在da.Fill(ds)中遇到错误。我想在保存表格之前检查CNIC。如果存在,则应提交表单,否则应显示CNIC无效。

public partial class ReportCrime : System.Web.UI.Page
       {
         MySqlConnection con = new MySqlConnection();
         MySqlCommand cmd = new MySqlCommand();
         DataSet ds = new DataSet();
     protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=localhost;Initial Catalog=fir;Integrated Security=True;User Name=root;Password=;";
        con.Open();
        }
     protected void btnSubmit_Click(object sender, EventArgs e)
        {

        MySqlCommand cmd = new MySqlCommand("select count(CNIC) from reportcrime where CNIC=(txtCnicNo.Text.ToString())", con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(ds);
        int i = ds.Tables[0].Rows.Count;
        if (i > 0)
        {
MySqlCommand acmd = new MySqlCommand("insert into reportcrime" + "(Name,CNIC,Phone1,Phone2,Email,Gender,City,Address,CrimeType,CrimeDetail)values(@Name,@CNICNO,@ContactNo.1,@ContactNo.2,@EmailAddress,@Gender,@CityName,@PostalAddress,@CrimeType,@CrimeDetail)", con);

                   acmd.Parameters.AddWithValue("@Name", txtName.Text);
                   acmd.Parameters.AddWithValue("@CNICNO", txtCnicNo.Text.ToString());
                   acmd.Parameters.AddWithValue("@ContactNo.1", txtcontactNo1.Text);
                   acmd.Parameters.AddWithValue("@ContactNo.2", txtcontactNo2.Text);
                   acmd.Parameters.AddWithValue("@EmailAddress", txtEmail.Text);
                   acmd.Parameters.AddWithValue("@Gender", ddlGender.SelectedItem.Value);
                   acmd.Parameters.AddWithValue("@CityName", txtCityName.Text);
                   acmd.Parameters.AddWithValue("@PostalAddress", txtaddress.Text);
                   acmd.Parameters.AddWithValue("@CrimeType", ddlCrimeType.SelectedItem.Value);
                   acmd.Parameters.AddWithValue("@CrimeDetail", txtCrimeDetails.Text);

                  acmd.ExecuteNonQuery();
         }

        else
        {
            MessageBox.Show("CNIC is not Valid");
        }

    }

 }

1 个答案:

答案 0 :(得分:0)

如果从文本框中传递的值只是字符串,则应该可以使用


MySqlCommand cmd = new MySqlCommand("select count(CNIC) from reportcrime where CNIC='"+Convert.ToString(txtCnicNo.Text)+"'", con);