0

在我的代码中插入了非常必要的行之后-

    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);
        }
     }
4

1 回答 1

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.
于 2013-11-04T20:42:29.637 回答