1,105,254 Community Members

How to insert date in datagrid column in C#

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello there,
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;

        da.UpdateCommand.ExecuteNonQuery();


    }

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.

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 
private void insertDate()
{
     da.UpdateCommand = new SqlCommand("UPDATE WaynokaLogger SET Date = @Date", cs);
     da.UpdateCommand.Parameters.Add("@Date", SqlDbType.Date).Value = dt;
     da.UpdateCommand.ExecuteNonQuery();
Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

I just need to somehow select that specific row for that user, then update his/her date column value

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
1
 

Thx for Voting :(

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;
da.UpdateCommand.ExecuteNonQuery();
Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Did not work i am afraid:( Can you update a null column value? Or should it be an insert command?

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

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.

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

insertDatetime = "UPDATE WaynokaLogger SET Date = GETDATE() WHERE AccountID =" + newString;
da.UpdateCommand = new SqlCommand(insertDatetime, cs);


Code above sort of works in sql and in my app, but its doesnt populate my "DATE" column for that specific member :( Any ideas?

Let me REPHRASE, the above code works in sql, but not in my C# app.

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Btw insert command didnt work either

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

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.

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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)
2. FirstName
3. LastName
4. AccountID
5. Membership

Anyway to work with this? Like:
UPDATE WaynokaLogger SET Date = GetDate() WHERE ID + @ID ????

Member Avatar
SyncMaster170
Junior Poster
110 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

Ignore this comment

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: