C# .net winforms data insert error -
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.data.sqlclient; namespace crudwithlogin { public partial class student_registration : form { sqlconnection conn = new sqlconnection(@"data source=(localdb)\mssqllocaldb;attachdbfilename=c:\users\android\documents\data.mdf;integrated security=true;connect timeout=30"); public student_registration() { initializecomponent(); } private void student_registration_load(object sender, eventargs e) { label1.text = "you logged in "+((form)this.mdiparent).controls["label1"].text; } private void button1_click(object sender, eventargs e) { conn.open(); sqlcommand cmd = conn.createcommand(); cmd.commandtype = commandtype.text; cmd.commandtext = "insert student values ('" + textbox1.text + "','" + textbox2.text + "''" + datetimepicker2.value.tostring() + "','" + textbox4.text + "','" + combobox2.text + "','" + textbox6.text + "','" + datetimepicker1.value.tostring() + "')"; cmd.executenonquery(); conn.close(); messagebox.show("data added successfully. "); } } }
hello experts creating windows forms app using c#, trying insert data database getting error of values , columns not match though entering right value right attribute.
i have 8 attributes (id auto increment), , remaining 7 columns entering 7 values error of value not match.
please me solve problem. have attached code , screenshots of problem
error shown here:
database schema screenshot here
('" + textbox1.text + "','" + textbox2.text + "''" + datetimepicker2.value.tostring() + "','" //missed , ------------------------------------^
also use command.parameters in future , avoid problems ! protected sqlinjection
. here example how define params.
sqlcommand cmd = conn.createcommand(); cmd.commandtype = commandtype.text; cmd.commandtext = @"insert tablenamefromdb values (@id, @title)"; cmd.parameters.addwithvalue("@id", youridvalue); cmd.parameters.addwithvalue("@title", yourtitlevalue); cmd.executenonquery(); conn.close();
also don't share 1 connection in app, use multiple connections. connection pool friend. check using block , use auto disposing of idisposable objects( example: sqlconnection
).
Comments
Post a Comment