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.
Alok SaxenaPosted Apr 9, 2015, 8:50 AM
If issue resolved by my suggestion, please marked it.
Thanks
Alok Saxena
Alok SaxenaPosted Aug 20, 2014, 4:15 AM
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
R JPosted Aug 13, 2014, 8:26 PM
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.
Azarudeen Ibn Liyakath AliPosted Aug 5, 2014, 8:12 AM
I didnt use sqlbulkcopy with the data table dtproducts. Please let me know where am going wrong
Azarudeen Ibn Liyakath AliPosted Aug 5, 2014, 3:37 AM
Khan Abrar AhmedPosted Aug 5, 2014, 3:25 AM
Azarudeen Ibn Liyakath AliPosted Aug 5, 2014, 3:20 AM
Error in foreach statement (Error: dtProducts.Rows Does not exist in current context)
Khan Abrar AhmedPosted Aug 5, 2014, 3:17 AM
Azarudeen Ibn Liyakath AliPosted Aug 5, 2014, 3:04 AM
When I give the following, error disappears
Correct me If am wrong?
Khan Abrar AhmedPosted Aug 5, 2014, 2:48 AM
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 AliPosted Aug 5, 2014, 2:30 AM
Update ParcelInf Set Status='Open',Remarks='Pending' where RequestID=1;
But still the code which you have provided is not updating in SQL
Posted Aug 4, 2014, 10:42 AM
Azarudeen Ibn Liyakath AliPosted Aug 4, 2014, 10:38 AM
I dont know where I am going wrong
Posted Aug 4, 2014, 8:47 AM
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 AliPosted Aug 4, 2014, 7:54 AM
private void BindGrid()
Posted Aug 4, 2014, 7:00 AM
replace my code ds.Table[0] to dtProducts
String Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
}
Azarudeen Ibn Liyakath AliPosted Aug 4, 2014, 6:52 AM
Posted Aug 4, 2014, 6:25 AM
private void BindGrid()
String Constr = "Data Source=WIN-A876U316VGA;integrated security=true;Initial Catalog=CUIDinfo";
}
Thanks
Kailash
Azarudeen Ibn Liyakath AliPosted Aug 4, 2014, 5:42 AM
I think I m not giving Update query properly in BindGrid.
I have attached the excel file also.
Posted Aug 4, 2014, 4:59 AM
By
Azarudeen Ibn Liyakath AliPosted Aug 4, 2014, 3:25 AM
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, 3:09 AM
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