c# - How to pass Datatable to SQL Server using asp.net -


i working on asp.net web application passing datatable asp.net application sql server stored procedure.

my table in sql server

student(id bigint, name nvarchar(max), reg bigint). 

in table, id primary key , auto incremented. c# code pass datatable stored procedure on button click is:

protected void btnsubmit_click(object sender, eventargs e) {     try     {         datatable dt = new datatable("student");          dt.columns.add("reg", typeof(long));         dt.columns.add("name", typeof(string));          // create new row         (int = 0; < 3; i++)         {             datarow newrow = dt.newrow();              if (i == 0)             {                 newrow["name"] = "raunak";                 newrow["reg"] = convert.toint64(1); ;             }             if (i == 1)             {                 newrow["name"] = "vikash";                 newrow["reg"] = convert.toint64(1);             }             if (i == 2)             {                 newrow["name"] = "deepak";                 newrow["reg"] = convert.toint64(1);             }              dt.rows.add(newrow);         }          dt.acceptchanges();          string constring = configurationmanager.connectionstrings["constring"].connectionstring;         sqlconnection cnn = new sqlconnection(@"data source=.\sqlexpress; initial catalog=buildererp; integrated security=true;");                     sqlcommand selectcommand = new sqlcommand("student_insert_update_delete", cnn);         selectcommand.commandtype = commandtype.storedprocedure;         sqlparameter tvpparam = selectcommand.parameters.addwithvalue("@student", dt);         tvpparam.sqldbtype = sqldbtype.structured;          cnn.open();          int xx = selectcommand.executenonquery();          if (xx > 0)         {             lblmsg.text = "data inserted.";         }         else         {             lblmsg.text = "data insertion failed.";         }         cnn.close();      }     catch (exception ex)     {         throw (ex);     } } 

my stored procedure in t-sql :

create procedure [dbo].[student_insert_update_delete] (     @student [dbo].[studenttableval] readonly ) begin    insert student(name,reg)          select name,reg @student         --commit transaction   end 

i have created table type as:

create type studenttableval table (     name nvarchar(max),     reg bigint )  

but when click button insert datatable student table error:

error converting data type nvarchar bigint.
data table-valued parameter "@student" doesn't conform table type of parameter.

please me someone.

the thing can think of it's not mapping column name index try invert column definition , see happens:

    dt.columns.add("name", typeof(string));     dt.columns.add("reg", typeof(long)); 

hope works.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -