Update SQL Server from asp.net -


what's error on when run it's give me error

string or binary data truncated. statement has been terminated.

it's updating using id take dropdown list.

code:

protected void page_load(object sender, eventargs e) {     if (!ispostback)     {         string constr = "data source=yazan-pc ; initial catalog=elder ; user = sa ; pwd =****;";         sqlconnection con = new sqlconnection(constr);         string sql = "select * users;";         con.open();         sqldataadapter da = new sqldataadapter(sql, con);         datatable dt = new datatable();         da.fill(dt);         con.close();         datarow dr = dt.newrow();         dr["id"] = "0";          dt.rows.insertat(dr, 0);         ddlid.datasource = dt;          ddlid.datavaluefield = "id";         ddlid.databind();     } }  protected void btnupdate_click(object sender, eventargs e) {     string constr = "data source = yazan-pc ;" +                    "initial catalog = elder;" +                    "user = sa ; pwd = ****;";     sqlconnection con = new sqlconnection(constr);     string sql =     "update users set name=@name , gender=@gender , email=@email ,usertype=@usertype, birthdate=@birthdate , password=@password, repassword=@repassword id=@id;";    con.open();     sqlcommand cmd = new sqlcommand(sql, con);      cmd.parameters.addwithvalue("@name", txtname.text);     cmd.parameters.addwithvalue("@gender", rblgender.selectedvalue);     cmd.parameters.addwithvalue("@email", txtemail.text);     cmd.parameters.addwithvalue("@usertype", rblusertype.selectedvalue);     cmd.parameters.addwithvalue("@birthdate", txtbirthdate.text);     cmd.parameters.addwithvalue("@password", txtpassword.text);     cmd.parameters.addwithvalue("@repassword", txtrepassword.text);     cmd.executenonquery();     con.close(); } 

that's table structure in sql server:

id           int           not null primary key identity, name         nvarchar(50)  not null , gender       char(1)       not null , email        nvarchar(50)  not null ,  usertype     char(1)       not null , birthdate    date          not null , password     nvarchar(50)  not null ,  repassword   nvarchar(50)  not null  

you need check length of strings before pass them sql. if given value column exceeds specified length, statement fail.

for example, name defined varchar(50), string in @name must less or equal 50 characters. 51 or more cause statement fail.

a simple if statement this, e.g.:

if (txtname.text.length > 50) {     // throw error, truncate string, etc. } 

again, you'll need check each string length less or equal length in db column.

it advisable check length of input on client side (via javascript or other methods) - way can provide immediate feedback user , can correct before submit information.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -