Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used a Datagridview, I fetch my table data into the Datagridview.
And I add a Select Checkbox column and date column into the same Datagridview.
When I will check checkbox that time that row should be save into the database.
For date column I am using datetime picker control into the datagridview (not separately on the form).
When I debug it all column able to save but not date.
Now I am not able to save my row into the database.

What I have tried:

I have used this code for datetime picker add in datagridview.
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
	DateTimePicker dtp = new DateTimePicker();

	if (e.ColumnIndex == 1)
	{

		dtp = new DateTimePicker();

		dataGridView1.Controls.Add(dtp);

		dtp.Format = DateTimePickerFormat.Short;

		Rectangle oRectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

		dtp.Size = new Size(oRectangle.Width, oRectangle.Height);

		dtp.Location = new Point(oRectangle.X, oRectangle.Y);

		dtp.Visible = true;
	}
}
This code is for Save button to save that checked row into the database.
C#
private void Btnsaveandpass_Click(object sender, EventArgs e)
{

	foreach (DataGridViewRow dr in dataGridView1.Rows)
	{
		bool checkboxselected = Convert.ToBoolean(dr.Cells["Column1"].Value);

		if (checkboxselected)
		{
			string insert = "insert into TBLDailyRecord values( @date ,@brandname, @generatedcode, @perpieceprice, @percentage, @soldprice)";
			SqlCommand cmd = new SqlCommand(insert, con);

			cmd.Parameters.AddWithValue("@date",dr.Cells[1].Value ); // I am getting error here.



			cmd.Parameters.AddWithValue("@brandname", dr.Cells[2].Value);

			cmd.Parameters.AddWithValue("@generatedcode", dr.Cells[3].Value);
			cmd.Parameters.AddWithValue("@perpieceprice", dr.Cells[4].Value);
			cmd.Parameters.AddWithValue("@percentage", dr.Cells[5].Value);
			cmd.Parameters.AddWithValue("@soldprice", dr.Cells[6].Value);
			con.Open();
			cmd.ExecuteNonQuery();

			con.Close();

		}
		label1.Text = "Selected rows save";
	}
Posted
Updated 22-Jun-20 4:47am
v2
Comments
MadMyche 18-Jun-20 16:27pm    
Your code has a comment of "I am getting error here"... what is the error
Member 14867079 19-Jun-20 21:07pm    
the parameterized query '(@date nvarchar(4000), @brandname nvarchar(12), @generated nva' expects the parameter '@date'. which is not supplied
MadMyche 19-Jun-20 21:12pm    
That means the value being retrieved from dr.Cells[1].Value is null, in which case the parameter will not be added
Member 14867079 19-Jun-20 21:17pm    
yes right. Now what can i do. I need a help
#realJSOP 22-Jun-20 10:43am    
My first guess is to find out why dr.Cells[1].Value is null, and fix it. My guess is that the cell value is a string, and your SQL is expecting a DateTime.

BTW, when you insert into a table, you really should specify the columns that you will be inserting data for. Your SQL statement is not guaranteed to work the way it's currently written.

1 solution

My first guess is to find out why dr.Cells[1].Value is null, and fix it. My guess is that the cell value is a string, and your SQL is expecting a DateTime.

BTW, when you insert into a table, you really should specify the columns that you will be inserting data for. Your SQL statement is not guaranteed to work the way it's currently written.
 
Share this answer
 

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