保存和更新语句不再有效

时间:2013-11-04 20:25:20

标签: c# asp.net sql visual-studio-2010

将非常必要的行插入我的代码 -

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
      try {


        GridViewRow row = GridView1.SelectedRow; 

            AccountNumber.Text = (string)row.Cells[0].Text;
           .....
            if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim()) != null)
            {
                DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();

            }
      }

      catch (Exception ex)
      {
          Console.WriteLine("{0} Exception caught.", ex);
      }

    }

没有抛出任何错误,所有字段都填充在相应的下拉框和文本框中。但是我的保存和插入语句不再有效。帮助!

  protected void AgentSave_Click(object sender, EventArgs e)
    {
    try
        {

            SqlConnection con = new SqlConnection("XX");
            SqlCommand command = con.CreateCommand();
            command.CommandText =
                "Insert into ABC values('" + AccountNumber.Text + "','" + Name.Text + "','" + Address1.Text + "','" + Address2.Text + "', '" + Address3.Text + "','" + PhoneNumber.Text + "','" + FaxNumber.Text + "','" + DropDownListCurrency.Text + "')";
            con.Open();
            command.ExecuteNonQuery();  
            con.Close();


            Response.Redirect("main.aspx");

            }

        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex);
        }
     }

1 个答案:

答案 0 :(得分:0)

因为这可能不是答案。在评论

中传递代码更改变得非常困难

你需要像这样处理你的SqlConnection:

        using (var con = new SqlConnection("XX"))
        {
            SqlCommand command = con.CreateCommand();
            command.CommandText = "INSERT ABC VALUES(@AccountNumber, @Name, ";//...you need to fill in all the parameters
            command.Parameters.Add("@AccountNumber", SqlDbType.Text);//Pick proper SqlDbType...whatever it is I dunno
            command.Parameters["@AccountNumber"].Value = AccountNumber.Text;
            //rinse and repeat for rest of params
            con.Open();
            command.ExecuteNonQuery();                
        }//using will automatically correctly close and dipose of the connection when con goes out of scope.