I am displaying values from a database into a dropdownlist. but I am unable to get the corresponding values into the DDL.
if (dt.Rows.Count > 0)
{
DataTable dt1 = new DataTable();
dt1 = bll.getnewid(TextBox1.Text);
if (dt1.Rows.Count > 0)
{
Session["pid"] = dt1.Rows[0]["NewidColumn"].ToString();
Session["email"] = dt1.Rows[0]["EmailID"].ToString();
Session["gender"] = dt1.Rows[0]["Gender"].ToString();
Session["mobile"] = dt1.Rows[0]["MobileNo"].ToString();
Session["country"] = dt1.Rows[0]["Country"].ToString();
Session["state"] = dt1.Rows[0]["State"].ToString();
}
and I am displaying like this
DropDownList1.Text = Session["country"].ToString();
DropDownList2.Text = Session["state"].ToString();
I am able to get the country and state values in the datatable. but I am unable to display them in the DDL.
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
Dropdownlist2.databind();
Dropdownlist1.databind();
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
You need to set the SelectedValue or SelectedIndex property of your dropdownlist.
try like this
DropDownList1.Items.Add("yourvalue");
DropDownList1.Items.Add(Session["country"].ToString());
Try This Code :
DropDownList1.Items.Insert(0, new ListItem(Session["country"].ToString(), "0"));
DropDownList1.DataBind();
DropDownList2.Items.Insert(0, new ListItem(Session["state"].ToString(), "0"));
DropDownList2.DataBind();
What is the need of using sessions ..?
Instead try this..hope it works.
After this line of code (dt1 = bll.getnewid(TextBox1.Text);)
use this two lines instead of sessions.
DropDownList1.DataSource=dt1;
DropDownList1.DataTextField="Country";
DropDownList1.DataValueField="Country";
DropDownList1.DataBind();
DropDownList2.DataSource=dt1;
DropDownList2.DataValueField="Country";
DropDownList2.DataValueField="Country";
DropDownList2.DataBind();
Related
**hello
Error adding items to the Grid.
please guide me**
Model1Container Mobl = new Model1Container();
JadvalSabtenam Sabt = new JadvalSabtenam();
Sabt.name = TextBox1.Text;
Sabt.family = TextBox2.Text;
Mobl.AddToJadvalSabtenamSet(Sabt);
Mobl.SaveChanges();
GridView1.DataSource = Sabt;
GridView1.DataBind();
GridView1.DataSource = Sabt;
GridView1.DataBind();
Sabt seems to be single object. To bind DataGrid you need to have collection like List<JadvalSabtenam > or BindingList<JadvalSabtenam>
Try that:
List<JadvalSabtenam > dataSource = new List<JadvalSabtenam> {Sabt};
GridView1.DataSource = dataSource;
GridView1.DataBind();
Reason for that is: What Grid should do with single object? When it's collection each item is corresponding to one row, and each property to column which makes perfect sense.
thank you. very well.
My problem was solved with your code.
List<JadvalSabtenam> dataSource = new List<JadvalSabtenam> { Sabt };
GridView1.DataSourceID = "";
GridView1.DataBind();
I am storing some column headers from an excel worksheet to a data table and extracting those column names/headers to populate my dropdown list in the aspx page. But sometimes I'm getting System.Data.DataRowView and sometimes it shows "Modified". I am using my datatable as DataSource for this Dropdownlist and the databinding is regularly failing. Can anyone help me with the code? Just a specimen so as to check where I am going wrong?
Here's the code:
protected void Button2_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (Path.GetExtension(FileUpload1.FileName) == ".xlsx")
{
DataTable dt = new DataTable();
ExcelPackage package = new ExcelPackage(FileUpload1.FileContent);
dt = package.ToDataTable(); //Datatable data from excel file
ListItem l = new ListItem();
for (int i = 0; i < dt.Columns.Count; i++)
{
DropDownList1.DataSource = dt;
l.Text = (dt.Columns[i].ColumnName).ToString();
l.Value = (dt.Columns[i].ColumnName).ToString();
DropDownList1.DataTextField = l.Text;
DropDownList1.DataValueField = l.Value;
DropDownList1.Items.Add(l);
DropDownList1.DataBind();
}
}
}
}
You don't need to add the items to the dropdownlist if you set the datatable as datasource, as this action will do that for you. You just have to set the property that will display the desired text and the hidden value of each item. Since I don't know the structure of your Datatable, I will post a sample code of my project:
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT Id, Name FROM Projects"), cn);
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
This operation will already add all the items stored in the datatable into the dropdownlist
EDIT: If the DataTable structure is dynamic/unknown, you will have to add the items by using a loop such as this:
DropDownList1.Items.Clear();
DataTable dt = new DataTable();
ExcelPackage package = new ExcelPackage(FileUpload1.FileContent);
dt = package.ToDataTable(); //Datatable data from excel file
for (int i = 0; i < dt.Columns.Count; i++)
{
//Now depending on whether you will have to access them by value or not:
DropDownList1.Items.Add(new ListItem(dt.Columns[i].ColumnName)); //Without Value
DropDownList1.Items.Add(new ListItem(dt.Columns[i].ColumnName, i)); //With a numeric value that will serve like an index
}
DropDownList1.Items.Clear();
DataTable dt = new DataTable();
ExcelPackage package = new ExcelPackage(FileUpload1.FileContent);
dt = package.ToDataTable(); //Datatable data from excel file
for (int i = 0; i < dt.Columns.Count; i++)
{
//Now depending on whether you will have to access them by value or not:
DropDownList1.Items.Add(new ListItem(dt.Columns[i].ColumnName)); //Without Value
DropDownList1.Items.Add(new ListItem(dt.Columns[i].ColumnName, i)); //With a numeric value that will serve like an index
}
i want to fill a combobox with data from the database when the page load
I had written the code as below
private void QuotationForm_Load(object sender, EventArgs e)
{
MessageBox.Show("hghjgvhg");
comboboxload();
}
public void comboboxload()
{
OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobecode from jobcodemastertable",oleDbConnection1);
OleDbDataReader reader = oleDbCommand1.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("jobpk", typeof(int));
dt.Columns.Add("jobcode", typeof(string));
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
oleDbConnection1.Close();
}
it doesnot deturns an error or exception but doesnot load the combobox with data values
try this
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
You may need to bind datatable's view with combo box
cmbjobcode.DataSource = dt.DefaultView;
You're missing the DataBind method
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
//here
cmbjobcode.DataBind();
oleDbConnection1.Close();
You have to call DataBind method on your combo. Thats why its not populating.
hello I have a code that pulls the data to the gridview using a dataset, what is the best way to check if the Gridview is empty and if it is to not throw an error.. Right now my gridview has the setting to show a message if its empty.. but I just want to null and empty check after attempting to get the data in the dataset
Students students = new Students();
DataSet studentsList = students.GetAllStudents();
GridView1.DataSource = studentsList;
GridView1.DataBind();
If I understand your question correctly, why not just check if the DataSet is empty, before you bind it to the GridView?
If it is, just don't bind it.
DataSet studentsList = students.GetAllStudents();
bool empty = IsEmpty(studentsList); // check DataSet here, see the link above
if(empty)
{
GridView1.Visible = false;
}
else
{
GridView1.DataSource = studentsList;
GridView1.DataBind();
}
You can count the returned row if its has data or not:
DataSet studentsList = students.GetAllStudents();
if(studentList.Tables[0].Rows.Count > 0) //COUNT DATASET RECORDS
{
GridView1.DataSource = studentsList;
GridView1.DataBind();
}
else
{
lblError.Text = "NO RECORDS FOUND!";
}
Regards
I am using database table as data source for my dropdown list. How do I add an extra item in my dropdown list for example "select state".
currently my code is:
dropdownlist1.datasource=dt;
dropdownlist1.datavaluefield="countryid";
dropdownlist1.datatextfield="countryname";
dt is a datatable , "countryid" & "countryname" are columns
you can do some thing like
dropdownlist1.Items.Insert(0, new ListItem("Select country", "0"));
You can do it like this
dropdownlist1.DataSource = dt;
dropdownlist1.DataTextField = "countryname";
dropdownlist1.DataValueField = "countryid";
dropdownlist1.DataBind();
dropdownlist1.Items.Insert(0, new ListItem("select state", "0"));