Insert ID into SQL from C# Combo box -
i have dropdown(combo) box in c# programme i'm writing. -it pulls through info table in database:
application_id | application ---------------|-------------- 1 |name1 2 |name2 3 |name3
the code dropdown box looks this:
sqlconnection conn = new sqlconnection(sqlcon.dbcon); conn.open(); sqlcommand sc = new sqlcommand("select * application", conn); sqldatareader reader; reader = sc.executereader(); datatable dt = new datatable(); dt.columns.add("applicationid", typeof(int)); dt.columns.add("application_name", typeof(string)); dt.load(reader); app.valuemember = "applicationid"; app.displaymember = "application_name"; app.datasource = dt; conn.close();
basically, user selects name of application , presses 'insert' button. want post relating id of application table in database. (accounts table). -i did have setup primary-foreign key relationship test removed relationship.
my code inserting database this:
sqlconnection conn = new sqlconnection(sqlcon.dbcon); conn.open(); using (sqlcommand aquery = conn.createcommand()) { aquery.commandtext = "insert accounts(accountname,application_id,num_users) values(@param1,@param2,@param3)"; aquery.parameters.add(new sqlparameter("@param1", account.text)); aquery.parameters.add(new sqlparameter("@param2", app.valuemember)); aquery.parameters.add(new sqlparameter("@param3", users.text)); aquery.executenonquery(); }
my issue, i'm getting exception occur when press insert. (everything compiles)
the exception states:
error conversion failed when converting nvarchar value 'applicationid' info type int.
-i have tried: cast convert
converting valuemember int in app before insert statement, still same error.
what doing wrong? -its not trying convert actual string 'valuemember' int it??
i think need selectedvalue retrieve selected value combo box, so:
app.selectedvalue
selectedvalue property: gets or sets value of fellow member property specified valuemember property.
you can set breakpoint on retrieving value check returned create sure number.
c# sql sql-server sql-insert
No comments:
Post a Comment