Click here to Skip to main content
16,009,255 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview filled from textboxes and comboboxes and I want from datagridview
to insert data into database with stored procedure.
Here you can find my stored procedure and code for button click.
When I execute project I get this error. Can anyone help me to fix this error.
Stored procedure
SQL
create PROCEDURE [dbo].[usp_InsertIntoTable]
@fid int,
@lid int,
@kid int,
@pid int,
@date datetime,
@type nvarchar(50),
@qty nvarchar(50),
@remarks nvarchar(500)
AS
begin
INSERT INTO Table1
VALUES
(
	@fid,
	@kid,
	@pid,
	@date, 
	@type,
	@remarks
)

INSERT INTO Table2
VALUES
(
	@fid,
	@lid,
	@qty
)


end


C# code on button click
C#
private void btnSave_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("usp_InsertIntoTable", BaseDAL.Connection);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < dgvRez.Rows.Count; i++)
{
	cmd.Parameters.Add(new SqlParameter("@fid", dgvRez.Rows[i].Cells["FID"].Value));
        cmd.Parameters.Add(new SqlParameter("@lid", dgvRez.Rows[i].Cells["LBook"].Value));
        cmd.Parameters.Add(new SqlParameter("@kid", dgvRez.Rows[i].Cells["KClient"].Value));
        cmd.Parameters.Add(new SqlParameter("@pid", dgvRez.Rows[i].Cells["PEmployee"].Value));
        cmd.Parameters.Add(new SqlParameter("@date", dgvRez.Rows[i].Cells["Date"].Value));
        cmd.Parameters.Add(new SqlParameter("@type", dgvRez.Rows[i].Cells["Type"].Value.ToString()));
        cmd.Parameters.Add(new SqlParameter("@qty", dgvRez.Rows[i].Cells["Quantity"].Value.ToString()));
        cmd.Parameters.Add(new SqlParameter("@remarks", dgvRez.Rows[i].Cells["Remarks"].Value.ToString()));
        cmd.ExecuteNonQuery();// Here I get the error (Conversion failed when converting the nvarchar value 'remarks' to data type int.)
}
}
Posted
Comments
yourfriendaks 14-Jun-13 7:22am    
have u Chaecked The Datatype Of Remarks In table
CHill60 14-Jun-13 7:43am    
I agree !
BulletVictim 14-Jun-13 7:38am    
You could always just try making the remarks variable a normal varchar(max) type.
CHill60 14-Jun-13 7:45am    
Try calling the procedure from query analyser (or whatever your equivalent sql environment is). If you get the same error then it's column 6 on your Table1 defined as int instead of varchar. However, if it works in sql then you should look further at your btnSave code
Sunasara Imdadhusen 14-Jun-13 7:54am    
What is your problem or error??

1 solution

C#
private void btnSave_Click(object sender, EventArgs e)
       {
           int chkLoop;

           try
           {
               chkLoop = dataGridView1.Rows.Count;
               //chkLoop = chkLoop - 1;
               MessageBox.Show(chkLoop.ToString());
               foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                   //MessageBox.Show(chkLoop.ToString());
                   //MessageBox.Show(row.co.ToString());
                   if (chkLoop != 0)
                   {
                       db.command.Parameters.Add("@ItemCode", SqlDbType.VarChar).Value = row.Cells["Category Code"].Value.ToString();
                       db.command.Parameters.Add("@ItemName", SqlDbType.VarChar).Value = row.Cells["Category"].Value.ToString();
                       //MessageBox.Show("" + row.Cells["Category Code"].Value.ToString());
                       db.Adapter("sp_NewItem", true);
                       chkLoop = chkLoop - 1;

                   }
               }
               MessageBox.Show("New Item added successfully.", "Successfully done", MessageBoxButtons.OK, MessageBoxIcon.Information);
               db.Adapter("select ItemName as [Item Name] from NewItem", "dt");
               dataGridPreviousList.DataSource = db.dataSet.Tables["dt"];
               //dataGridPreviousList.DataSource = db.Adapter("select ItemName as [Item Name] from NewItem");
           }
           catch (Exception err)
           {
               MessageBox.Show(err.Message);
           }
       }
 
Share this answer
 
Comments
Syed Hamza 27-Nov-19 0:22am    
I want to search value in datagrid coulum from database.If any one help i will be thankfull.Thanks in advance.

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