mysql - C# display a string from a sql database -
i trying make small contact book takes contacts details mssql database.it has 3 tables: contacts, last_talk(last time talked contact+short description of discussion), , table(that has both primary keys first 2 tables )
on form(tab of tabcontrol) display contacts, have added 2 listboxes, 1 loads , displays contacts names, , second listbox loads "last talk" list every contact select depending how many "talks" had contact.
now trying display inside label.text , inside richtextbox.text database, when select field in "conversation list" listbox.
my database has 3 tables: 1 contacts, 1 discussions/talks, 1 creating relation between first 2 tables.
here part of code:
private void populateselectedtalk() { string query = "select * lasttalk id = @id"; using (connection = new sqlconnection(connectionstring)) using (sqlcommand command = new sqlcommand(query, connection)) using (sqldataadapter adapter = new sqldataadapter(command)) { command.parameters.add("@id", sqldbtype.int); command.parameters["@id"].value = lstconversationlist.selectedvalue; connection.open(); sqldatareader rdr = command.executereader(); while (rdr.read()) { lbllasttalk.text = rdr["lasttalksubject"].tostring(); rtxtlasttalkdescription.text = rdr["lasttalkdescription"].tostring(); } rdr.close(); connection.close(); } } private void lstconversationlist_selectedindexchanged(object sender, eventargs e) { populateselectedtalk(); }
i had similar problem posted here: c# displaying sql database object's name in label.text property on "selectedindexchanged" event
and after added
command.parameters.add("@id", sqldbtype.int); command.parameters["@id"].value = lstconversationlist.selectedvalue;
it worked.
however, when i'm trying use lstconversationlist.selectedvalue; following exception
an exception of type 'system.invalidcastexception' occurred in system.data.dll not handled in user code additional information: failed convert parameter value datarowview int32.
the exception thrown line: sqldatareader rdr = command.executereader();
when hover mouse on "listbox.selectedvalue" in debug mode in previous issue, value of "1", when hover on "listbox.selectedvalue" in code posted above "system.datarowview"
here code previous question(it works):
private void populatecontactlabels() { string query = "select * contact id = @id"; using (connection = new sqlconnection(connectionstring)) using (sqlcommand command = new sqlcommand(query, connection)) using (sqldataadapter adapter = new sqldataadapter(command)) { command.parameters.add("@id", sqldbtype.int); command.parameters["@id"].value = lstcontactlist.selectedvalue; connection.open(); sqldatareader rdr = command.executereader(); while (rdr.read()) { lblcontactname.text = rdr["name"].tostring(); lblcompany.text = rdr["company"].tostring(); lbloccupation.text = rdr["occupation"].tostring(); lblphonenumber.text = rdr["phonenumber"].tostring(); lblemail.text = rdr["email"].tostring(); } rdr.close(); connection.close(); } }
what missing/doing wrong? please help!
edit: code sets items in lstconversationlist
private void populatetalklist() { string query = "select a.lasttalksubject lasttalk " + "inner join contactlasttalk b on a.id = b.lasttalkid " + "where b.contactid = @contactid"; using (connection = new sqlconnection(connectionstring)) using(sqlcommand command = new sqlcommand(query,connection)) using (sqldataadapter adapter = new sqldataadapter(command)) { command.parameters.addwithvalue("@contactid", lstcontactlist.selectedvalue); datatable lasttalktable = new datatable(); adapter.fill(lasttalktable); lstconversationlist.displaymember = "lasttalksubject"; lstconversationlist.valuemember = "id"; lstconversationlist.datasource = lasttalktable; } }
the problem arises fact set datasource of lstconversationlist
datatable , forget set valuemember
property name of column contains value retrieve selectedvalue property.
when set datasource property datatable, each item in listbox.items collection datarowview. if don't set valuemember
property, selectedvalue has no way know column read return value. returns whole datarowview instance, and, of course, not integer searching for.
so when fill lstconversationlist not forget write
private void populatetalklist() { ' note. need add id field select query string query = @"select a.id, a.lasttalksubject lasttalk inner join contactlasttalk b on a.id = b.lasttalkid b.contactid = @contactid"; using (connection = new sqlconnection(connectionstring)) using(sqlcommand command = new sqlcommand(query,connection)) using (sqldataadapter adapter = new sqldataadapter(command)) { command.parameters.addwithvalue("@contactid", lstcontactlist.selectedvalue); datatable lasttalktable = new datatable(); adapter.fill(lasttalktable); lstconversationlist.displaymember = "lasttalksubject"; lstconversationlist.valuemember = "id"; lstconversationlist.datasource = lasttalktable; } }
Comments
Post a Comment