Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have a sql database table with Num1, Num2, Num3 and Date columns.
In my 'SqlDataAdapter I'm NOT binding the Date column to a datagrid but only Num columns(SELECT Num1, Num2, Num3 FROM tblTestTable1). However I am adding additional column to a datagridview which will be used as a datatime picker. So basically, customer will click on that and pick the date from the calendar.
C#
CalendarColumn col = new CalendarColumn();
this.dataGridView1.Columns.Add(col);
col.Name = "Date";

Once customer picked the date we want to record that to the database. Num1 to Num1 column and so on. At the end, date from datetimepicker column to the Date column in database. And that is a problem right there.
C#
da.InsertCommand = new SqlCommand("INSERT INTO tblTestTable1 VALUES  (@Num1,@Num2,@Num3,@Date", conn);

da.InsertCommand.Parameters.Add("@Num1", SqlDbType.Int);
da.InsertCommand.Parameters.Add("@Num2", SqlDbType.Int);
da.InsertCommand.Parameters.Add("@Num3", SqlDbType.Int);
da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value =  col;

it doesn't like the last command. Any ideas?
Thanks in advance.
JML_
Posted
Updated 5-Apr-11 4:34am
v2

When you get errors either when compiling or running code and you need help, it is really helpful to the folks here on CP if you tell us what the error is. The exact wording where possible. Otherwise anything said in answer is really a guess.

Anyway, here's my guess.
Try splitting the line into two parts the first exactly like your others, just adding the parameter.
Then the second to set the value:
C#
da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime);
da.InsertCommand.Parameters["@Date"].Value =  col;


BTW: If you could enclose any code snippets in <pre>code goes here</pre> tags it makes them much easier to read.
 
Share this answer
 
Comments
JML_ 5-Apr-11 11:35am    
I did chamge the code to what you've got here and it gives me an error " Failed to convert parameter value from a CalendarColumn to a DateTime " :(
when I change the code to:
da.InsertCommand.Parameters["@Date"].Value = Convert.ToDateTime(col);
Unable to cast object of type 'CalendarColumn' to type 'System.IConvertible'.
So, now I'm lost. :(
JML_
Henry Minute 5-Apr-11 11:50am    
You are passing a DataGridViewCalendarColumn as the value, so it is no surprise that you get an error.

You haven't shown the method that your code comes from (if it is an event handler or a stand-alone method) so it is difficult to give specific advice. What you need to do is get the cell from the row you are interested in into a suitable variable (myCalCell) and use myCalCell.Value.
The col variable is of a type CalendarColumn and must be of type DateTime.

You must retrieve the date / time picked by the user from the control, and not insert the control in your database.

Have fun!
Eduard
 
Share this answer
 
Comments
JML_ 5-Apr-11 11:37am    
how do i do that? can you help?
JML_

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