I am exporting data from excel and show it in gridview and when I click submit, it needs to be updated in existing table.
Below is my excel data
S.No. RequestID Subdivision No Parcel No CUID Status Remarks
1 5 42 55 55 Open PendingI have successfully exported and show it in Gridview. Now I want to update the same in the existing table(Tablename:ParcelInf).
Below is my table detail
Table ParcelInf(
[RequestID] [int] IDENTITY(1,1) NOT NULL,
FormattedRequestID AS ('CUID' + RIGHT('00' + CAST(RequestID AS VARCHAR(10)),10)),
[SubdivisionNo] [nvarchar](20) NOT NULL,
[ParcelNo] [nvarchar](20) NOT NULL,
[Region] [nvarchar](20) NULL,
[City] [nvarchar](20) NULL,
[Zone] [nvarchar](20) NOT NULL,
[CoordinateSystem] [nvarchar](20) NOT NULL,
[CUID] [nvarchar](20) NULL,
[Status] [nvarchar](20) NULL,
[Remarks] [nvarchar](20) NULL,
[UpdateDate] [date] NULL,
[RequestedBy] [nvarchar] (20) NULL,
FOREIGN KEY (RequestID) REFERENCES RequestInf (RequestID)
Could you please help me on this.

Posted Aug 3, 2014, 11:09 PM EDT
You can use sqlbulkcopy to insert your bulk data (gridview data) into sql, kindly go throw the link will help you
http://www.c-sharpcorner.com/UploadFile/87b416/performing-bulk-copy-in-ado-net/
Kindly mark as answer, if this helps you
Thanks,
Kailash
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 3, 2014, 11:25 PM EDT
I want to use the update query to update the detail in the existing table.
I already tried to using the bulk data
Could you please help me how I can use the update query in this case
Posted Aug 4, 2014, 12:59 AM EDT
By
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 1:42 AM EDT
I think I m not giving Update query properly in BindGrid.
I have attached the excel file also.
Posted Aug 4, 2014, 2:25 AM EDT
private void BindGrid()
String Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
}
Thanks
Kailash
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 2:52 AM EDT
Posted Aug 4, 2014, 3:00 AM EDT
replace my code ds.Table[0] to dtProducts
String Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
}
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 3:54 AM EDT
private void BindGrid()
Posted Aug 4, 2014, 4:47 AM EDT
try this
private void BindGrid()
{
String Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
SqlConnection con = new SqlConnection(Constr);
con.Open();
foreach (DataRow DR in dtProducts.Rows)
{
String UpdateQuery = "Update ParcelInf Set Status='"+ DR["Status"]+"',Remarks='"+DR["Remarks"]+"' where RequestID="+DR["Remarks"];
SqlCommand cmd = new SqlCommand(UpdateQuery, con);
cmd.CommandText = UpdateQuery;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
}
con.Close();
}
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 6:38 AM EDT
I dont know where I am going wrong
Posted Aug 4, 2014, 6:42 AM EDT
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 10:30 PM EDT
Update ParcelInf Set Status='Open',Remarks='Pending' where RequestID=1;
But still the code which you have provided is not updating in SQL
Khan Abrar AhmedRank #778 · 3K reputationPosted Aug 4, 2014, 10:48 PM EDT
private void BindGrid()
{
string Constr = null;
SqlConnection con ;
SqlCommand cmd ;
string UpdateQuery = null;
Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
con = new SqlConnection(Constr);
foreach (DataRow DR in dtProducts.Rows)
{
con.Open();
UpdateQuery = "Update ParcelInf Set Status='"+ DR["Status"]+"',Remarks='"+DR["Remarks"]+"' where RequestID="+DR["Remarks"];
cmd = new SqlCommand(UpdateQuery, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
}
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 11:04 PM EDT
When I give the following, error disappears
Correct me If am wrong?
Khan Abrar AhmedRank #778 · 3K reputationPosted Aug 4, 2014, 11:17 PM EDT
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 11:20 PM EDT
Error in foreach statement (Error: dtProducts.Rows Does not exist in current context)
Khan Abrar AhmedRank #778 · 3K reputationPosted Aug 4, 2014, 11:25 PM EDT
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 4, 2014, 11:37 PM EDT
Azarudeen Ibn Liyakath Ali30 reputationPosted Aug 5, 2014, 4:12 AM EDT
I didnt use sqlbulkcopy with the data table dtproducts. Please let me know where am going wrong
R J192 reputationPosted Aug 13, 2014, 4:26 PM EDT
Try use LinqToExcel (https://code.google.com/p/linqtoexcel/) to retrieve data from Excel to c#object which is more faster and you dont have to have a dependency on OLEDB etc.
Alok SaxenaRank #3,748 · 395 reputationPosted Aug 20, 2014, 12:15 AM EDT
I read the all thread. All are correct the issue in Binding() method.
What I feel in this method you are missing the code which fetch data into dtProducts (DataTable). Here in this code you are created a new DataTable and next two lines adding two columns.
And very next lines you are using foreach (DataRow DR in dtProducts.Rows).
There is a issue. Actually dtProducts is empty.
Regards
Alok Saxena
Alok SaxenaRank #3,748 · 395 reputationPosted Apr 9, 2015, 4:50 AM EDT
If issue resolved by my suggestion, please marked it.
Thanks
Alok Saxena