Populate combobox
-
Hi all. Im trying to load data into a combo box during form load.The items Im trying to load are from a database. Now here is my little code that gives me errors. protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = str; String str1 = "Select * from Filter"; con.Open(); cm.Connection = con; dtr = cm.ExecuteReader(); if (dtr.Read()) { // Load FilterByKeys onto the combo box cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem**** lblMessage.Text = "Good. All the values are loaded"; Thanx.
kagiso
-
Hi all. Im trying to load data into a combo box during form load.The items Im trying to load are from a database. Now here is my little code that gives me errors. protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = str; String str1 = "Select * from Filter"; con.Open(); cm.Connection = con; dtr = cm.ExecuteReader(); if (dtr.Read()) { // Load FilterByKeys onto the combo box cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem**** lblMessage.Text = "Good. All the values are loaded"; Thanx.
kagiso
What a mess
matjame wrote:
con.ConnectionString = str;
what is str, and why is a member variable named so badly ?
matjame wrote:
String str1 = "Select * from Filter";
You never use this anywhere, so it's irrelevant to the rest of the code. What is con, cm and dtr ? Why do you have badly named member variables like this, that probably could be local, and are not named in a way that means anything ?
matjame wrote:
cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem****
What's the problem ? Does it not compile ? Does it blow up ? What's the error message ? This is also not going to do more than add one value into the combo box. That's what the Text property will do. To add items, make a collection the datasource, and if it's not a collection of strings, set the DisplayMember property. All of this looks to me like you're making it up as you go and hoping it will work. Intellisense is no substitute for reading MSDN or buying a good book as a reference.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi all. Im trying to load data into a combo box during form load.The items Im trying to load are from a database. Now here is my little code that gives me errors. protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = str; String str1 = "Select * from Filter"; con.Open(); cm.Connection = con; dtr = cm.ExecuteReader(); if (dtr.Read()) { // Load FilterByKeys onto the combo box cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem**** lblMessage.Text = "Good. All the values are loaded"; Thanx.
kagiso
DataTable status = new DataTable(); status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ; cmbStatus.DisplayMember = "Status_Name"; cmbStatus.ValueMember = "Status_Id"; give the displaymember and valuemember to the combobox'x object here cmbStatus is the combobox
My small attempt...
-
What a mess
matjame wrote:
con.ConnectionString = str;
what is str, and why is a member variable named so badly ?
matjame wrote:
String str1 = "Select * from Filter";
You never use this anywhere, so it's irrelevant to the rest of the code. What is con, cm and dtr ? Why do you have badly named member variables like this, that probably could be local, and are not named in a way that means anything ?
matjame wrote:
cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem****
What's the problem ? Does it not compile ? Does it blow up ? What's the error message ? This is also not going to do more than add one value into the combo box. That's what the Text property will do. To add items, make a collection the datasource, and if it's not a collection of strings, set the DisplayMember property. All of this looks to me like you're making it up as you go and hoping it will work. Intellisense is no substitute for reading MSDN or buying a good book as a reference.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi all. Im trying to load data into a combo box during form load.The items Im trying to load are from a database. Now here is my little code that gives me errors. protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = str; String str1 = "Select * from Filter"; con.Open(); cm.Connection = con; dtr = cm.ExecuteReader(); if (dtr.Read()) { // Load FilterByKeys onto the combo box cboExtension.Text = (dtr["FilterKey"]).ToString();<<*****Problem**** lblMessage.Text = "Good. All the values are loaded"; Thanx.
kagiso
:)what a mess is this!! "cbo.extension.text" will never populate items in to combobox control, this only displays the specified text if it is already there in combobox items collection. to populate the items u should specifie { combobox.items.add(dtr["filterKey"].tostring()); }
-
DataTable status = new DataTable(); status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ; cmbStatus.DisplayMember = "Status_Name"; cmbStatus.ValueMember = "Status_Id"; give the displaymember and valuemember to the combobox'x object here cmbStatus is the combobox
My small attempt...
-
:)what a mess is this!! "cbo.extension.text" will never populate items in to combobox control, this only displays the specified text if it is already there in combobox items collection. to populate the items u should specifie { combobox.items.add(dtr["filterKey"].tostring()); }
cboExtension.items.add(dtr["filterKey"].tostring()); items gives this error "System.Web.UI.WebControl.DropdownList doesnt contain definition for items." Then to .tostring() "object doesnt contain a definition for string Can you generate a code that would work. NB. The Items in the database are jus A.B.C til Z So, it suppose to load A B C Till Z Thank you for your help.A nice code would be nice.Im an amature in this
kagiso
-
sujithkumarsl wrote:
status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ;
:confused::|
status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ; actually ExecuteNonQuery_SP("Select * from Filter"); is a method which takes quaery as parameter and returns the datatable. i used this code in my proj and its working
My small attempt...
-
cboExtension.items.add(dtr["filterKey"].tostring()); items gives this error "System.Web.UI.WebControl.DropdownList doesnt contain definition for items." Then to .tostring() "object doesnt contain a definition for string Can you generate a code that would work. NB. The Items in the database are jus A.B.C til Z So, it suppose to load A B C Till Z Thank you for your help.A nice code would be nice.Im an amature in this
kagiso
please try the exable which i gave to you. that will work which field from db, you want to add in combobox?
My small attempt...
-
cboExtension.items.add(dtr["filterKey"].tostring()); items gives this error "System.Web.UI.WebControl.DropdownList doesnt contain definition for items." Then to .tostring() "object doesnt contain a definition for string Can you generate a code that would work. NB. The Items in the database are jus A.B.C til Z So, it suppose to load A B C Till Z Thank you for your help.A nice code would be nice.Im an amature in this
kagiso
using sytem.oledb; using system.windows.forms; oledbconnection connect=new oledbconnection; oledbcommand command=new oledbbcommand("Select 'field_name' from 'tabel' where 'condition'", connect); oledbreader reader=command.executeReader(); reader.read(); { combobox1.items.add(reader[0].tostring()); } reader.close(); connect.close();
-
please try the exable which i gave to you. that will work which field from db, you want to add in combobox?
My small attempt...
-
using sytem.oledb; using system.windows.forms; oledbconnection connect=new oledbconnection; oledbcommand command=new oledbbcommand("Select 'field_name' from 'tabel' where 'condition'", connect); oledbreader reader=command.executeReader(); reader.read(); { combobox1.items.add(reader[0].tostring()); } reader.close(); connect.close();
using sytem.oledb; using system.windows.forms; oledbconnection connect=new oledbconnection; oledbcommand command=new oledbbcommand("Select 'field_name' from 'tabel' ***There are only two fields. ID & FilterKey and Im using FilterKey so no need for where ***** where 'condition'", connect); oledbreader reader=command.executeReader(); reader.read(); { combobox1.items.add(reader[0].tostring()); } reader.close(); connect.close();
kagiso
-
sujithkumarsl wrote:
status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ;
:confused::|
-
using sytem.oledb; using system.windows.forms; oledbconnection connect=new oledbconnection; oledbcommand command=new oledbbcommand("Select 'field_name' from 'tabel' where 'condition'", connect); oledbreader reader=command.executeReader(); reader.read(); { combobox1.items.add(reader[0].tostring()); } reader.close(); connect.close();
-
status = ExecuteNonQuery_SP("Select * from Filter");//getting the value cmbStatus.DataSource =status ; actually ExecuteNonQuery_SP("Select * from Filter"); is a method which takes quaery as parameter and returns the datatable. i used this code in my proj and its working
My small attempt...
sujithkumarsl wrote:
actually ExecuteNonQuery_SP("Select * from Filter"); is a method which takes quaery as parameter and returns the datatable. i used this code in my proj and its working
Two things: 1) There is no method named
ExecuteNonQuery_SP
in the framework, and therefore using it in an example to a newer programmer is just going to confuse them more! This method is most likely defined in your own project, or is part of a data access framework that you use. 2) The name of the method would imply that it executesIDbConnection.ExecuteNonQuery
which does not "return a table". If your method really does take a SQL clause as a parameter and return a table it should be namedExecuteDataTable_SP
Please take the time to consider the usefulness of the code and the target audience before posting a (useless) code snippet. -
Ok, its not taking the using system.Windows.forms; and the combobox1.items.add(reader[0].tostring()); says its doesnot have a definition for Add. Everything else was what I did before
kagiso
hi if its a web application, it wont take windows.forms, and u wont get combobox control also.... in such case, use " using system.web.ui.webcontrols; " { dropdownlist1.items.add(reader[0].tostring()); } this will defenetely work:)
-
hi if its a web application, it wont take windows.forms, and u wont get combobox control also.... in such case, use " using system.web.ui.webcontrols; " { dropdownlist1.items.add(reader[0].tostring()); } this will defenetely work:)