i am writing the below code to get the data in the combo box but it does not show any data in the combo box so please help..... below is my code for filling combo box..
string qry = "select ctid,city from city order by city";
cmd = new SqlCommand(qry, con);
ds.Clear();
ad = new SqlDataAdapter(qry,con);
ad.Fill(ds);
MessageBox.Show(ds.Tables[0].Rows[1]["city"].ToString());
comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "city";
comboBox1.ValueMember = "ctid";
in the messagebox after the ad.fill(ds) i can see the value inside the table but it is not reflecting in the combo box...
I have used the same logic in my app just the difference was to specify the CommandType property as 'Text' and CommandText property as the query, in SqlCommand.
var sqlDs = new DataSet();
var sqlComm = new SqlCommand();
sqlComm.Connection = sqlCon;
sqlComm.CommandType = CommandType.Text;
sqlComm.CommandText = query;
sqlDa = new SqlDataAdapter(sqlComm);
sqlDa.Fill(sqlDs);
Also when you are specifying the display member and Value member then no need to specify the columns in select query, it still work. only for the performance basis you can specify the columns name.
Related
as stated in the title, I would appreciate some help with populating comboboxes with different columns of the same table
I don't have the actual source codes with me but I was able to populate one combobox with something like
SqlDataTable dt = new SqlDataTable();
SqlCommand comm = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
da.Fill(dt);
myCMB.DataSource = dt;
myCMB.DisplayMember = "display";
myCMB.ValueMember = "value;
but I have 5 comboboxes, and I didn't want to repeat the same blocks of code 5 times just for that. so I looked around and found some answers, including changing datasource to bindingsource, like so:
myCMB.DataSource = new BindingSource(da, "Column_Name");
but doing so would populate the combobox with each letter of the first item of the specified column (ie. if the first item in "Column_Name" is ABCD, my combobox options would be A,B,C,D)
So, I tried looking for more answers but I can't find any. is there a more efficient way of populating my comboboxes or do I really have to repeat essentially the same lines of code for each of them? if anyone can help, it would be greatly appreciated.
Use a copy of the datatable:
SqlDataTable dt = new SqlDataTable();
SqlCommand comm = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
da.Fill(dt);
myCMB.DataSource = dt.Copy();
myCMB.DisplayMember = "display";
myCMB.ValueMember = "value;
Try this:
DataTable dt = new DataTable();
dt.Columns.Add("Value");
dt.Columns.Add("Display");
string query = #"SELECT ID AS ID, Description AS CODE FROM [Tender] ORDER BY ID";
SqlDataReader sqlReader = new SqlCommand(query, DB.SQLConnection).ExecuteReader();
while (sqlReader.Read())
{
if (!string.IsNullOrEmpty(sqlReader["CODE"].ToString()))
dt.Rows.Add(sqlReader["ID"].ToString(), sqlReader["CODE"].ToString());
}
cbPayment1.DisplayMember = "display";
cbPayment1.ValueMember = "value";
cbPayment1.DataSource = dt;
I am just Trying to insert the data into the Sql From Selecting the item From the combobox in windows form.
But after the insertion in the database is does not show the value of selected item but it shows ("System.Data.DataRowView").
my code of insertion Is.
FORM LOAD-
SqlDataAdapter adp = new SqlDataAdapter("select * from CPP", con);
DataSet ds= new DataSet();
adp.Fill(ds);
comboBox10.DisplayMember = "Name";
comboBox10.ValueMember = "Id";
comboBox10.DataSource = ds.Tables[0];
On the BUTTON CLICK EVENT-
SqlCommand cmd = new SqlCommand("Insert into CPP (Name) values ('" + comboBox10.SelectedItem.ToString() + "')", con);
cmd.ExecuteNonQuery();
After the insertion of selective item.
It Shows In the database Table-("System.Data.DataRowView")
Any suggestion?
You can try below updated code.
var row = (DataRowView)comboBox10.SelectedItem;
SqlCommand cmd = new SqlCommand("Insert into CPP (Name) values ('" + row["Name"].ToString() + "')", con);
cmd.ExecuteNonQuery();
You need to set DisplayMember and ValueMember properties accorrdingly.
ComboBox1.DisplayMember = "Foo";
ComboBox1.ValueMember = "Bar";
If you don't specify these properties, a ListControl like ComboBox does not know what to display. So it displays just the type of the item as string (Type.FullName).
So the following code will not enter the loop to populate my list box. My list box shows "Select All" and test outputs "entering loop". What would cause the try not to fail but the loop not to execute either?
conn.Open();
OracleCommand executeQuery = new OracleCommand(sql, conn);
executeQuery.CommandType = CommandType.Text;
OracleDataReader dr = executeQuery.ExecuteReader();
lstInstructors.Items.Clear();
lstInstructors.Items.Add(new ListItem("Select All", "%"));
string test = "entering loop";
while (dr.Read())
{
test = "start reading items";
lstInstructors.Items.Add(new ListItem(dr.GetValue(0).ToString()));
test += dr.GetValue(0).ToString();
}
Where is the value for "sql" set?
A different way to accomplish populating populating a list box would be to fill a data table then bind it to the list box.
OracleDataAdapter da = new OracleDataAdapter();
DataTable dt = new DataTable();
var sql = "some sql string";
OracleCommand executeQuery = new OracleCommand(sql, conn);
using (da = new OracleDataAdapter(executeQuery))
{
da.Fill(dt);
}
lstInstructors.DataSource = dt;
lstInstructors.DataTextField = "Field Name from sql";
lstInstructors.DataValueField = "Field Value from sql";
lstInstructors.DataBind();
lstInstructors.Items.Insert(0, new ListItem("Choose", "Choose"));
lstInstructors.SelectedValue = "Choose";
be sure to include using System.Data
I've adapted this from the SqlDataAdapter so there may be some syntax differences.
I try to bind database data to the gridview in c# and asp.net. But I couldn't see the datas in the gridview.Rows are added to the gridview but they are empty. When I run that query in SQLServer, it gives the correct result.I didn't add or change any code to the asp part.Should I? I couldn't find where is the problem :( please help..
myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
command = new SqlCommand();
connect.Open();
command.Connection = connect;
string komut = "SELECT K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi FROM OduncIslemleri O,Kitap K WHERE O.kullaniciId=" + Session["id"] + " AND O.kitapId = K.id;";
try
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand = connect.CreateCommand();
sqlCommand.CommandText = komut;
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connect);
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//Create a DataTable to hold the query results.
DataTable dTable = new DataTable();
//Fill the DataTable.
sda.Fill(dTable);
GridView1.DataSource = dTable;
GridView1.DataBind();
}
catch (SqlException)
{
//Console.WriteLine(e.StackTrace);
}
reader.Close();
connect.Close();
Here is the correct answer :
myConnection = WebConfigurationManager.ConnectionStrings["KutuphaneConnectionString"].ConnectionString;
connect = new SqlConnection(myConnection);
string sorgu = "select K.ad,K.yazar,K.baskiNo,O.sonTeslimTarihi from Kitap K, OduncIslemleri O where O.kitapId = K.id and O.kullaniciId = "+ Session["id"];
SqlDataAdapter sadp = new SqlDataAdapter(sorgu, connect);
DataSet ds = new DataSet();
sadp.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
connect.Close();
I also used template fields in Gridview. Also autogeneratedFields should be true. I hope this helps to the people who have the same problem
watch for another event triggered after the bind that could be clearing the rows
Try creating a DataSet and populate that using Fill instead. I've never seen Fill used on a DataTable - and can't find that particular overload on MSDN. My suspicion is, though, that such an overload would not modify the existing schema of the DataTable (which, since it's only just been created prior to use in your example, would mean that it has no columns).
I think you have to use a BindingSource Control, you set the DataSource of it to the DataTable, and then set the GridView's DataSource to the BindingSource.
Using VS2005 with C#
I want to fill the combobox by using the table value.
Code
OdbcConnection con = new OdbcConnection();
OdbcCommand cmd;
con.ConnectionString = "";
con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
ds = new DataSet();
ada.Fill(ds);
combobox1.Items.Add(ds);
But no values was loading in the combobox, what wrong with my above mentioned code.
can any provide a solution for the probelm....
You do have something in your real connection string, right?
You're loading your data into a DataSet - that's a collection of tables and relations. How should the combobox know what table's data to display?? If the DataSet has multiple tables in it, you would have to additionally define which one of those to use. If the DataSet has only one table inside, then it's a waste of resources to use a DataSet in the first place.
If you only have a single set of data, use a DataTable instead:
con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
DataTable data = new DataTable();
ada.Fill(data);
// define the column to be used to display text in the combobox
combobox1.DataTextField = "FirstName";
// define the column to be used as the value for the selection in the combobox
combobox1.DataValueField = "CustomerID";
combobox1.DataSource = data;
combobox1.DataBind();