-2
 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.UpdateCommand = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=" +Label1.Text,mycon);
        myadp.UpdateCommand.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        myadp.UpdateCommand.Parameters.Add("@weblnk", SqlDbType.VarChar,80).Value = TextBox3.Text;
        myadp.UpdateCommand.Parameters.Add("@email", SqlDbType.VarChar,80).Value = TextBox4.Text;
        myadp.UpdateCommand.Parameters.Add("@cntct", SqlDbType.VarChar,20).Value = TextBox5.Text;
        myadp.UpdateCommand.Parameters.Add("@lctn", SqlDbType.VarChar,80).Value = TextBox6.Text;
        myadp.UpdateCommand.Parameters.Add("@cdscrptn", SqlDbType.VarChar,600).Value = TextBox7.Text;
        myadp.UpdateCommand.Parameters.Add("@bsnstp", SqlDbType.VarChar,40).Value = TextBox8.Text;
        myadp.UpdateCommand.Connection = mycon;
        mycon.Open();
        myadp.UpdateCommand.ExecuteNonQuery();
        mycon.Close();
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True");
        SqlDataAdapter myadp = new SqlDataAdapter();
        myadp.UpdateCommand = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=@cmpny", mycon);
        myadp.UpdateCommand.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        myadp.UpdateCommand.Parameters.Add("@cmpny", SqlDbType.VarChar, 50).Value = TextBox2.Text;
        myadp.UpdateCommand.Parameters.Add("@weblnk", SqlDbType.VarChar,80).Value = TextBox3.Text;
        myadp.UpdateCommand.Parameters.Add("@email", SqlDbType.VarChar,80).Value = TextBox4.Text;
        myadp.UpdateCommand.Parameters.Add("@cntct", SqlDbType.VarChar,20).Value = TextBox5.Text;
        myadp.UpdateCommand.Parameters.Add("@lctn", SqlDbType.VarChar,80).Value = TextBox6.Text;
        myadp.UpdateCommand.Parameters.Add("@cdscrptn", SqlDbType.VarChar,600).Value = TextBox7.Text;
        myadp.UpdateCommand.Parameters.Add("@bsnstp", SqlDbType.VarChar,40).Value = TextBox8.Text;
        myadp.UpdateCommand.Connection = mycon;
        mycon.Open();
        myadp.UpdateCommand.ExecuteNonQuery();
        mycon.Close();
    }

here I have parametrize cmpny too but its still not working

Eonasdan
  • 7,563
  • 8
  • 55
  • 82

3 Answers3

4

I assume that cmpny is a text field, so you need to enclose it in apostrophes:

WHERE [cmpny]='" + Label1.Text + "'",mycon);

However, forget this immediately. You should use parameters always.

WHERE [cmpny]=@cmpny", mycon);

and

myadp.UpdateCommand.Parameters.AddWithValue("@cmpny", TextBox1.Text);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
  1. Get rid of the unnecessary SqlDataAdapter.

  2. Get a real Database on your local server. The master database isn't intended for your data.

  3. Check the return value of ExecuteNonQuery. Maybe your assumed cmpny value doesn't exist in the table?

  4. Attach a minimum of Exception Handling to your code.

 

using (SqlConnection mycon = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=master;Integrated Security=True"))
{
    mycon.Open();
    using (SqlCommand cmd = new SqlCommand("Update [orgs] Set [fname]=@fname,[weblnk]=@weblnk,[email]=@email,[cntct]=@cntct,[lctn]=@lctn,[cdscrptn]=@cdscrptn,[bsnstp]=@bsnstp WHERE [cmpny]=@cmpny", mycon))
    {
        cmd.Parameters.Add("@fname", SqlDbType.VarChar, 50).Value = TextBox1.Text;
        // all the other params
        cmd.Parameters.Add("@bsnstp", SqlDbType.VarChar, 40).Value = TextBox8.Text;
        cmd.Parameters.Add("@cmpny", /*correct Datatype here*/).Value = Label1.Text;  // from a Label ?? how does it got there? You should take the value from the actual source
        int affectedRecords = cmd.ExecuteNonQuery();
    }
}
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Ralf
  • 1,216
  • 10
  • 20
0

Label1.Text should be parameterized too because it may contains some quotes. In a general way, you should parametize every user inputed value in SQL in order to avoid SQL injections.

You may have some error because of this so your update command is not working.

ToXinE
  • 308
  • 2
  • 13