c# - converts varchar to int while i am inserting to an sql table -
i have table, have named users, , whenever user using sign form want insert users.table credentials enters. problem that, somehow, database trying convert username(column) varchar int. insert query :
string query = "insert users(firstname, lastname, username, password, role, email, class) values('" + txtfn.text + "', '" + txtsn.text + "','" + txtusername.text + "','" + txtpassword.text + "', '" + cbrole.text + "','" + txtemail.text + "','" + cbclass.text + "')";
the error pops-ups everytime user trying sign up
how can prevent conversion happening or other way deal error? in advance.
here how use sql parameters , specify types. safier , more precise , handle conversions types (such datetime):
string query = "insert users(firstname, lastname, username, " + " password, role, email, class) " + " values(@fn, @sm, @user, @ps, @role, @email, @class)"; sqlcommand sqlcom = new sqlcommand(query, sqlconnection); // change types match sql column data types // set nvarchar sample var fnparma = new sqlparameter("@fn", sqldbtype.nvarchar); var smparma = new sqlparameter("@sm", sqldbtype.nvarchar); var userparma = new sqlparameter("@user", sqldbtype.nvarchar); var psparma = new sqlparameter("@ps", sqldbtype.nvarchar); var roleparma = new sqlparameter("@role", sqldbtype.nvarchar); var emailparma = new sqlparameter("@email", sqldbtype.nvarchar); var classparma = new sqlparameter("@class", sqldbtype.nvarchar); // here set each value converting specified type. fnparma.value = txtfn.text; smparma.value = txtsn.text; // continue params. if have int column use int32.parse(text); sqlcom.executenonquery();
Comments
Post a Comment