Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i update my datagrid if there's no ID columns in my dataset? Unique would be "Date" column. Date is entered by user. tblNUMBERSBS_1 is a binding source. Data is entered to table with no problem, then text fields 1 to 5 can be updated but no Date since that unique field of course. any ideas?


SQL
da.UpdateCommand = new SqlCommand("UPDATE tblNUMBERS_1 SET FirstNum = @FirstNum, SecondNum = @SecondNum, ThirdNum = @ThirdNum, FourthNum = @FourthNum, FifthNum = @FifthNum WHERE Date = @Date", cs);

da.UpdateCommand.Parameters.Add("@FirstNum", SqlDbType.Int).Value = int.Parse(txtFirstNum.Text);
da.UpdateCommand.Parameters.Add("@SecondNum", SqlDbType.Int).Value = int.Parse(txtSecondNum.Text);
da.UpdateCommand.Parameters.Add("@ThirdNum", SqlDbType.Int).Value = int.Parse(txtThirdNum.Text);
da.UpdateCommand.Parameters.Add("@FourthNum", SqlDbType.Int).Value = int.Parse(txtFourthNum.Text);
da.UpdateCommand.Parameters.Add("@FifthNum", SqlDbType.Int).Value = int.Parse(txtFifthNum.Text);

da.UpdateCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value = ds.Tables[0].Rows[tblNUMBERSBS_1.Position][5];

          
cs.Open();
x = da.UpdateCommand.ExecuteNonQuery();
cs.Close();





Thanks..
Posted
Comments
yesotaso 18-May-11 10:25am    
Thats the purpose of PRIMARY KEY and/or NOT NULL. :) Besides allowing NULL for a field that is supposed to be unique beats the purpose.

1 solution

Unique would be "Date" column
As you say it is unique, it can work as an ID! Use this for inserting if the same date does not exists from before. If it exists then use update.
 
Share this answer
 
Comments
yesotaso 18-May-11 11:09am    
Well, that field is nullable as far as I can see so it cannot be used for identification.
Sandeep Mewara 18-May-11 11:28am    
Where do you see it nullable? I don't see that piece of information actually. It would be in table design. Right?
yesotaso 18-May-11 15:54pm    
Umm sorry about that "Data is entered to table with no problem, then text fields 1 to 5 can be updated but no Date since that unique field of course"
I misunderstood that part.
JML_ 18-May-11 11:35am    
Well, Date is set NOT NULL. It actually does the update but it picks the wrong row. :(
Sandeep Mewara 18-May-11 11:49am    
If it is unique, how it is picking the wrong row? Did you try to run the raw SQL directly on database and see what it gave?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900