c# - Column data missing when i import data from .csv excel to grid view in Asp.net -
i trying export excel sheet data grid view in asp.net site. grid view populating data missing excel sheet column data emp_code
.
excel data:
rendered gridview:
code populating asp:gridview
:
protected void btnsave_click(object sender, eventargs e) { if (fileupload1.filename != "") { try { fileupload1.saveas(server.mappath("~/appraisal/" + fileupload1.filename)); cvspath = null; string constr = null; //constr = system.configuration.configurationmanager.connectionstrings("connstr").tostring; constr = configurationmanager.connectionstrings["connectionstring"].connectionstring; sqlconnection dwconn = new sqlconnection(constr); //con.open() cvspath = server.mappath("~/appraisal/" + fileupload1.filename); //response.write(cvspath) fileupload1.dispose(); // dim constr string system.data.oledb.oledbconnection csvconn = new system.data.oledb.oledbconnection(); //constr = system.configuration.configurationmanager.connectionstrings("connstr").tostring //csvconn.connectionstring = ("provider=microsoft.ace.oledb.12.0;;data source=" + cvspath + ";extended properties=\"excel 8.0;hdr=yes;\""); csvconn.connectionstring = @"provider=microsoft.ace.oledb.12.0;data source=" + cvspath + ";extended properties=excel 12.0;persist security info=false"; // csvconn.open(); system.data.oledb.oledbcommand csvcmd = new system.data.oledb.oledbcommand(); csvcmd.commandtext = ("select * [sheet1$]"); //csv file being in localdir //assign cmd connection csvcmd.connection = csvconn; //create csv reader system.data.oledb.oledbdatareader csvrdr = null; //insert csv contents reader csvrdr = csvcmd.executereader(); //open sql connection dwconn.open(); //create sqlbulk copy insert csv reader db table sqlbulkcopy sqlbulk = new sqlbulkcopy(dwconn); sqlbulk.destinationtablename = "[dbo].[mstappointmentregister]"; sqlbulk.writetoserver(csvrdr); //close csv connection csvrdr.close(); csvconn.close(); //close sql connection dwconn.close(); lblerror.visible = true; lblerror.text = "file uploaded successfully"; if (lblerror.text == "file uploaded successfully") { fillgridfromexcelsheet(); } } catch (exception ex) { response.write("<br />" + ex.message); } //response.redirect("upload_closingprice.aspx?act=1"); } else { lblerror.visible = true; lblerror.text = "please select file"; } }
gridview
, sqldatasource
markup:
<asp:gridview id="excelgridview" runat="server" cssclass="table table-striped" visible="false" autogeneratecolumns="false" datakeynames="srid" onpageindexchanging="gridview1_pageindexchanging" onrowcancelingedit="gridview1_rowcancelingedit" onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating"> <columns> <asp:commandfield showeditbutton="true" headertext="modify" /> <asp:commandfield showdeletebutton="true" headertext="remove" /> <asp:boundfield datafield="srid" headertext="srid" /> <asp:boundfield datafield="emp_code" headertext="emp_code" /> <asp:boundfield datafield="letter" headertext="letter" /> <asp:boundfield datafield="letter" headertext="letter" /> <asp:boundfield datafield="name" headertext="name" /> <asp:boundfield datafield="first_name" headertext="first_name" /> <asp:boundfield datafield="address_line1" headertext="address_line1" /> <asp:boundfield datafield="address_line2" headertext="address_line2" /> <asp:boundfield datafield="address_line3" headertext="address_line3" /> <asp:boundfield datafield="doj" headertext="doj" /> <asp:boundfield datafield="designation" headertext="designation" /> <asp:boundfield datafield="ctc" headertext="ctc" /> <asp:boundfield datafield="ctc_in_words" headertext="ctc in words" /> </columns> </asp:gridview> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="data source=103.21.58.193;initial catalog=jig_match_db;user id=jig_match_db;password=atheros@7412" deletecommand="delete [mstappointmentregister] [srid] = @srid" selectcommand="select * [mstappointmentregister]" updatecommand="update [mstappointmentregister] set [name] = @name, [first_name] = @first_name, [address_line_1] = @address_line_1, [address_line_2] = @address_line_2, [address_line_3] = @address_line_3, [doj] = @doj, [designation] = @designation, [ctc] = @ctc, [ctc_in_words] = @ctc_in_word [srid] = @srid" insertcommand="insert [mstappointmentregister] ([name],[first_name],[address_line_1],[address_line_2],[address_line_3],[doj],[designation],[ctc],[ctc_in_words]) values (@name,@first_name,@address_line_1,@address_line_2,@address_line_3,@doj,@designation,@ctc,@ctc_in_words)"> <deleteparameters> <asp:parameter name="srid" type="int64" /> </deleteparameters> <updateparameters> <asp:parameter name="emp_code" type="string" /> <asp:parameter name="letter" type="string" /> <asp:parameter name="name" type="string" /> <asp:parameter name="first_name" type="string" /> <asp:parameter name="address_line1" type="string" /> <asp:parameter name="address_line2" type="string" /> <asp:parameter name="address_line3" type="string" /> <asp:parameter name="doj" type="string" /> <asp:parameter name="designation" type="string" /> <asp:parameter name="ctc" type="string" /> <asp:parameter name="ctc_in_words" type="string" /> </updateparameters> </asp:sqldatasource>
database schema table mstappointmentregister
:
create table [dbo].[mstappointmentregister]( [srid] [bigint] identity(1,1) not null, [emp_code] [varchar](150) null, [letter][varchar](200) null, [name] [varchar](150) null, [first_name] [varchar](100) null, [address_line_1] [varchar](100) null, [address_line_2] [varchar](150) null, [address_line_3] [varchar](100) null, [doj] [varchar](100) null, [designation] [varchar](150) null, [ctc] [varchar](150) null, [ctc_in_words] [varchar](150) null, constraint [pk_mstappointmentregister] primary key clustered ( [srid] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary]
please me out ?
in sqlbulkcopy
column mappings case-sensitive regardless of case sensitivity setting in target db.
as msdn notes,
column mappings define mapping between data source , target table.
if mappings not defined—that is, columnmappings collection empty—the columns mapped implicitly based on ordinal position. work, source , target schemas must match. if not, invalidoperationexception thrown.
if columnmappings collection not empty, not every column present in data source has specified. not mapped collection ignored.
in case excel returns column emp_code
, table in database has column emp_code
. mapping fails emp_code != emp_code
.
msdn link: sqlbulkcopycolumnmapping class
Comments
Post a Comment