C# DropDownList listing number value - c#

I have a dropdownlist that fills like:
cmd.CommandText = "select * from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
Now I want to read the listing number of the selected item(in order to place it in a query).
So, while my dropdownlist has values like "Oxford University", "MIT University", "Simon Fraser University" etc, I want clicking an item from the list, store the value of this item in the list. To be more specific, if clicking the first item, then
int number = 1. if clicking the second item, then
int number = 2.
Is it possible to be done with something like:
DropDownList1.SelectedItem.????;
Thank you for your time!

You didn't bind item's value to dropdownlist, so you need to bind it.
like this
DropDownList1.DataValueField = ds.Tables[0].Columns["Column name that contain the value of Item"].ToString();
and then you can use to get it's value like as follow
string item_value = DropDownList1.SelectedValue;

If you want selected Value then use
DropDownList1.SelectedValue
and if you want selected item then use
DropDownList1.SelectedItem

In addition to Nitin Kumar answer, selected item text can be obtained by:
var selectedText = DropDownList1.SelectedItem.Text;
Selected Value
var selectedValue = DropDownList1.SelectedValue;

cmd.CommandText = "select * from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataTextField = ds.Tables[0].Columns["schoolName"].ToString();
DropDownList1.DataValueField = ds.Tables[0].Columns["SomeValueColumn or id column"];
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataBind();
and then you can use...
string id = DropDownList1.SelectedValue.ToString();
Try this.
cmd.CommandText = "select id,schoolName from dbo.pelischool";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt );
if (dt != null)
{
if (dt.Rows.Count > 0)
{
ddlPosition.DataSource = dt;
ddlPosition.DataTextField = "schoolName";
ddlPosition.DataValueField = "Id";
ddlPosition.DataBind();
}
}

Related

How to get first item from comboxbox in c#?

I fill one combobox with two columns from database with this code:
foreach (DataRow dr2 in dt2.Rows)
{
comboBox1.Items.Add(dr2["Station"].ToString() + " - " + dr2["Ime"].ToString());
}
dbconn.Close();
and combobox look like this:
Now I want to take only first item from this combobox (the numbers) and put into my query for data from database.
I want variable Stations to be with first item from combobox.
I try to get the first index from combobox, but when I debug I see value on the variable is -1 everytime..
The code:
var Stations = comboBox1.SelectedIndex = -1;
MySqlCommand cmd = new MySqlCommand("CALL aladin_surfex.Get_mod_cell_values_meteogram_cell('"+ dtnow +"', " + Stations + ", 5)", dbconn);
MySqlDataAdapter da = new MySqlDataAdapter();
dbconn.Open();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds);
dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
dataGridView1.DataSource = dt;
}
dbconn.Close();
I upload a picture from debug mode:
I want variable Stations to be with first item from combobox
Instead of
var stations = comboBox1.SelectedIndex = -1;
use
comboBox1.SelectedIndex = -1;
var stations = comboBox1.SelectedText; // or Text

Show only unique columns in repeater control using asp c#

I'm trying to bind repeater control from database. But i need to bind repeater with unique column.
eg.
Mumbai, Haryana, Goa, Haryana, Mumbai
to
Mumbai, Haryana, Goa
My code is given below.
SqlConnection con3 = new SqlConnection("myconnection");
string strcon = "select * from indiapincodes where statename =#statename";
cmd = new SqlCommand(strcon, con3);
da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("#statename", erouting);
ds = new DataSet();
da.Fill(ds, "emp");
Repeater1.DataSource = ds;
Repeater1.DataBind();
con3.Close();
con3.Dispose();
Above code is working good but i want to know how to filter column with unique value and fill in repeater. Thanks in advance.
string strcon = "SELECT DISTINCT columnname from indiapincodes WHERE statename=#statename";

Add "ALL" option as 1st Selection Of Combobox

I am in need of adding in the ALL option as the top choice of a combobox. I have tried this code below, but All is not addded, what must I alter in order to have that added?
string query = "select [empname] from [server].[dbo].[table]";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "tables");
cbotables.DisplayMember = "empname";
cbotables.Items.Insert(0, "All");
cbotables.DataSource = ds.Tables["tables"];
EDIT
I just realized a few things were not showing in my code...my connection string is declared above, and the contents of the combobox display as they should from the database, just no ALL option added.
Perhaps the simplest way would be to Insert a row into the DataTable assuming its only role is to be a DataSource:
// fill the datatable
dt.Load(cmd.ExecuteReader());
var dr = dt.NewRow();
dr["Id"] = -1;
dr["Name"] = "All";
dt.Rows.InsertAt(dr, 0);
cboEmp.DisplayMember = "Name";
cboEmp.ValueMember = "Id";
cboEmp.DataSource = dt;
It is more common to have a ValueMember with these so you can tell what was selected, thus the "Id" column. If the DataTable has other purposes you may not want to add fake data to it. For that, you could transfer the data to an anonymous list:
dt.Load(cmd.ExecuteReader());
// xfer data to anon collection
var myDS = dt.AsEnumerable()
.Select(q => new {Name = q.Field<String>("Name"),
Value = q.Field<Int32>("Id") }
)
.ToList();
// add ALL with fake id
myDS.Insert(0, new { Name = "ALL", Value = -1 });
cboEmp.DisplayMember = "Name";
cboEmp.ValueMember = "Value";
cboEmp.DataSource = myDS;
Either way gets the same results:
If you truly dont need the Id, you dont need an anon type and can just select the name and a List<string>

Datagrid failing to populate with Dataset - Input string not in correct format

I have a datagrid view in which I set value like this
string query = "select CustomerId,CustomerName from tbl_Customer where flag=0";
SqlDataAdapter daCustomer = new SqlDataAdapter(query, con);
daCustomer.Fill(ds, "Customer");
cmbCustomerName.DisplayMember = "CustomerName";
cmbCustomerName.ValueMember = "CustomerId";
cmbCustomerName.DataSource = ds.Tables["Customer"];
cmbCustomerName.ResetText();
but it gives me this error after this line
cmbCustomerName.DataSource = ds.Tables["Customer"];
when I set datasource. Please help me fix this.
Try this :
DataTable dt = new DataTable();
daCustomer.Fill(dt);
cmbCustomerName.DataSource = dt;

System.Data.DataRowView, c#

I've got a problem:
SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.User.name FROM dbo.User", con);
DataTable dt = new DataTable();
comboBox1.DataSource = dt;
da.Fill(dt);
It returned System.Data.DataRowView in every row.
I tried:
comboBox1.ValueMember = "idUser";
comboBox1.DisplayMember = "dbo.User.name"
and I get error:
Cannot bind to the new display member. Parameter name:
newDisplayMember
I want to get idUser and name.
Thanks
You need to return the ID in your SELECT statement and assign the DataTable as the DataSource after you have filled the DataTable:
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
You can then set up the ComboBox as such:
comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name"
UPDATE
If you want to access the selected text or value of the item selected in the ComboBox, then you will need to do something like:
DataRowView drvItem = comboBox1.SelectedItem as DataRowView;
if (drvItem != null)
{
label4.Text = drvItem["ID"].ToString();
label3.Text = drvItem["Name"].ToString();
}
Please use below
SqlDataAdapter da = new SqlDataAdapter("SELECT IDUser, Name FROM dbo.User", con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "IDUser";
comboBox1.DisplayMember = "Name";

Categories