Click here to Skip to main content
16,004,854 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
for (int y = 0; y < GridView1.Rows.Count - 0; y++)
{
  SqlCommand cmd2 = new SqlCommand("insertDeliveryNote_DetalisTBL2", con2);
 cmd2.CommandType = System.Data.CommandType.StoredProcedure;

 cmd2.Parameters.AddWithValue("@Customer_Id", LabelIdCustomer.Text);
 cmd2.Parameters.AddWithValue("@FK_DeliveryID", txtId.Text);
 cmd2.Parameters.AddWithValue("@Product_Id", GridView1.Rows[y].Cells[1].v);
  cmd2.Parameters.AddWithValue("@Product_Name", GridView1.Rows[y].Cells[2].ToString());
 cmd2.Parameters.AddWithValue("@Price", GridView1.Rows[y].Cells[3].ToString());
  
cmd2.ExecuteNonQuery();


What I have tried:

This code give me error - "Error converting data type nvarchar to int".
I try to insert all data from GridView1 to database after bind
Posted
Updated 7-May-17 6:19am
v3
Comments
Member 12695679 7-May-17 9:23am    
this the error
Error converting data type nvarchar to int.
[no name] 7-May-17 9:24am    
Don't you think that telling us what the error is, is kind of really really really important information to share?
CHill60 7-May-17 9:28am    
Was that exception generated by the line cmd2.ExecuteNonQuery();?
Somewhere you have a nvarchar column and you are trying to use it as if it was an integer. Post the code from the stored procedure insertDeliveryNote_DetalisTBL2 and include the table schema
Member 12695679 7-May-17 9:36am    
cmd2.Parameters.AddWithValue("@Product_Id", GridView1.Rows[y].Cells[0].ToString());
how can convert this row to int
CHill60 7-May-17 9:52am    
If you are going to reply use the "Reply" link otherwise the poster is not notified.
And do not repost the same question! Be patient.

I have answered this on your other post Convert row to integer in gridview[^]
 
Share this answer
 
Dear,

The above solution(by @Chill60) will solve your question. So follow the steps what he suggest.

--------------
If there will be more data in gridview then, for every row it will call the stored procedure which may cause of performance issue.

From the below link you can get the idea how you can pass whole grdview data to the stored procedure in one go with out calling stored procedure for each row.

Sending a DataTable to a Stored Procedure[^]
Passing a datatable to a Stored Procedure in SQL Server 2008[^]

Refer these link it may give ideas.
 
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