I am having trouble with my C# datagrid. Im working with sql. I have four columns in my datagrid: firstname, lastname, membership, date. So heres the design behind my app. Employee scans a card, hits update. Info pertaining to that member gets pulled from the sql database, then populated into my datagrid. Date gets added after. I did it this way because its the only way I know how to add it. Once that same members card gets scanned again, I am wanting the app to check the last time the card was scanned, and then display a message based on that. Heres my problem:
I NEED to add the date under the "Date" column, but am having a little trouble. Heres my code:
private void insertDate()
da.UpdateCommand = new SqlCommand("UPDATE WaynokaLogger SET Date = @Date", cs);
da.UpdateCommand.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = dt;
Now the above code works, only problem is that it updates all rows date values! Can someone please help me out? Major cred will be awarded. This is my first time integrating Sql with C#. And might I add its kind of a b!tch.
I will still help you out... what you have to do is to use WHERE clause to specify for which row you want to update the datetime. Do you have any Id, or some other unique column, so you can use the comparison?
Your query them will look:
da.UpdateCommand = new SqlCommand("UPDATE WaynokaLogger SET Date = @Date WHERE YourUniqueColumn = @uniqueColumn", cs);
da.UpdateCommand.Parameters.Add("@Date", SqlDbType.Date).Value = dt;
da.UpdateCommand.Parameters.Add("@uniqueColumn", SqlDbType.Int).Value = SomeUniqueValue;
sorry for the vote but you just changed one thing which I knew was not gonna help me out. But for the comment you just left I am giving it a big thumbs up. You made me think of using the unique id which would be, in my case anyway, AccountID column. Thanks again and I will report back to let you know if it worked.
Sure you can update a NULL value in a particular cell. Null only refers there is no data yet in the cell (at least it worked for me), maybe it differs form database to databsse, not sure about it.
If its still doesnt work, try with Insert command.
But insert only that field.
Code code it self has to be ok (the strucure I mean) but I dont know about your column names and the conitional clause (where).
Where did you get the where clause and check if it really exist in the database`s table.
kind of a newbie when it comes to sql statements, but an hour ago u posted that I needed a where clause to specify for which row i want to update the datetime? But I remade my sql table because I did not have a primary key column.
Heres my new columns:
1. ID(primary key)
Anyway to work with this? Like:
UPDATE WaynokaLogger SET Date = GetDate() WHERE ID + @ID ????
You dont need to use ID as Where clause, you can use a name, or lastname, or both or them, or some other field.
But the problem comes, when there are duplicates of names (or lastnames, or whatever field). Thats why we use ID column. Id is (must be) unique, and cannot be duplicated.
That why its strongly recommended to use for a where clause an id field.
But if you want to do an update and use a where clause, you have to know this particular id. You have to get it some how.
Are you able to get it?
If so, there is no problem, if not, you have to think of some way to get it.
Thats why its best to have to dataTable, where are all the fields inside (with all the rows (all the data)), so you have in one row: an id, name, lastname, and other columns.
So when you want to update some date, you get this particular row out, and now you are sure that an Id belongs only to the name and lastname from this row.
And this way you can simply do an update.
You know what I mean?
Read carefully, and try to do some examples, and let me know.