0
    public void Updatecottonpurchase(int slipno, int basicprice, int premium, int totalamountpaid, int weight, int totalamountbasic, int totalamountpremium, int yeildestimates, int farmercode)
    {
        SqlConnection sqlConn = new SqlConnection(@"Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");

        try
        {
            string sqlQuery = "UPDATE cottonpurchse SET slipno = '" + slipno + "' , basic price = '" + basicprice + "' , premium = '" + premium + "' , totalamountpaid = '" + totalamountpaid + "' , weight = '" + weight + "' , totalamountbasic = '" + totalamountbasic + "' , totalamountpremium = '" + totalamountpremium + "' , yeildestimated = '" + yeildestimates + "' WHERE farmercode = '" + farmercode + "'";
            SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
            sqlConn.Open();
            cmd.ExecuteNonQuery();
            sqlConn.Close();
        }


        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            sqlConn.Close();
        }

        finally
        {
            sqlConn.Close();
        }
    } 

this is what ive done now yet nothing happens! i want to beable to update the null values but nothing happens! please help

12
  • 1
    How are you trying to add/update the record? Commented Jun 22, 2011 at 17:17
  • im trying to do it through winforms! u see i have many textboxes tht are null now when i enter the values do i have to insert or update and how? Commented Jun 22, 2011 at 17:21
  • string queryString = "UPDATE cottonpurchase SET slipno=InputSlipNoHere WHERE farmercode=2"; try Commented Jun 22, 2011 at 17:22
  • What is your data access method? Commented Jun 22, 2011 at 17:26
  • 1
    @tanya: Databases do not contain textboxes. Do you mean that you have a form with TextBox instances, and some of those (non-null) textbox instances have a null Value? Are you trying to populate the textbox values with values from the database, or are you trying to create/update database rows with values from the textboxes? Or do you mean something completely different? Commented Jun 24, 2011 at 1:19

2 Answers 2

1

This SQL code:

UPDATE TABLE cottonpurchase SET  slipno= WHERE farmercode=

Does nothing, you need to add parameters,
see: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

You need to change the code into:

....
string queryString = 
     "UPDATE TABLE cottonpurchase SET slipno=@slipno WHERE farmercode=@farmercode"; 
try       
{          
  connection.Open();          
  SqlCommand command = new SqlCommand(queryString, connection);

  //define parameters used in command object
  SqlParameter param  = new SqlParameter();
  param.ParameterName = "@slipno";
  param.Value         = inputfromsomewhere;

  SqlParameter param  = new SqlParameter();
  param.ParameterName = "@farmercode";
  param.Value         = inputfromsomewhereelse;

  //add new parameter to command object
  command.Parameters.Add(param);


  int result = command.ExecuteNonQuery();          
  //if result = 1 the update is performed         
} 
......  
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add or choose a column for use as the primary key. The primary key should uniquely identify a row, and is used to locate the row to update.

4 Comments

no but i will be doing it for many rows! not only one infact for all the rows
please help me i really am stuck with this dont understand what to do
@Tanya - You need to post the C# you wrote that at least attempts it. If you don't understand the different between INSERT and UPDATE then do some research.
i know the difference i just dont know what to use to fill in those null values???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.