how to bind database table to dropdownlist value

Posted by vijaykumarecomnets-9645 under Regular Expressions on 3/30/2011 | Points: 10 | Views : 182286 | Status : [Member] | Replies : 8
hi i am beginner in asp.net i have small task
in my form one dropdownlist control is there so i want to bind database table to dropdownlist values and then
if i select any value in drpdownlist it display on grid view that value details
please send me exactly code and one more thing in my application developing in 3 tire architectur




Responses

Posted by: T.saravanan on: 3/30/2011 [Member] [MVP] Silver | Points: 25

Up
1
Down
Hi,

Try this...
//Bind DataTable value into DropDowmList..
//Here dtTable is your DataTable.
DropDownList1.DataSource  = dtTable;  

DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField= "Id";
DropDownList.DataBind();


//In your DropDownList SelectedIndexChanged Event...
string sId = DropDownList.SelectedValue.ToString();

string sName = DropDownList.SelectedItem.ToString();


Now you pass 'Id' (or) 'Name' into your query then get a value from database bind into GridView.

Note: 1) If you bind DropDownList value in your Page Load event means don't forget to call IsPostBack
2) Set DropDownList 'AutoPostBack' property is 'true'

Try this...if any more doubts ask me...

Thanks,
T.Saravanan

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 5/19/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
use this,

ddlEmpList.DataSource = blVisitor.GetEmployees();

ddlEmpList.DataTextField = "Name";
ddlEmpList.DataValueField = "Empid";
ddlEmpList.DataBind();


Here ddlEmpList is dropdownlist's name,blVisitor is a class library, GetEmployees is a method for fetching data from database. Then set tthe textField and ValueField.

GetEmployees() Method,

public DataSet GetEmployees()

{
dsVisitors = new DataSet();
try
{
QryString = "SELECT Firstname+' '+LastName AS Name,Empid FROM EmployeesProj";
dsVisitors = SqlHelper.ExecuteDataset(connString, CommandType.Text, QryString);
}
catch (Exception ex)
{
throw ex;
}
return dsVisitors;
}



Regards,
Susan

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 5/20/2011 [Member] Starter | Points: 25

Up
0
Down
<asp:dropdownlist id="ddl1" runat="Server" auotpostback="true" onselectedindexchanged="ddl1_onselectedindexchanged">
<asp:gridview id="gv1" runat="server">

in code behind

protected void ddl1_onselectedindexchanged(object sender,EventArgs e)
{
sqldataadapter da=new sqldataadapter("select * from t_name where col="+ddl1.selectedvalue);
datatable dt=new datatable();
da.fill(dt);
gv.datasource=dt;
gv.databind();
}

Thanks&Regards
LakshmiNarayana Nalluri.

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 5/20/2011 [Member] Starter | Points: 25

Up
0
Down
for filling dropdownlist values
in pageload
if(!ispostback())
{
bind();
}
publiv void bind()
{
sqldataadapter da=new sqldataadapter("select * from tname");
datatable dt=new datatable();
da.fill(dt);
ddl1.datasource=dt;
ddl1.datatextfield="col1";
ddl1.datavaluefield="col1";
ddl1.databind();
ddl1.items.insert(0,"Select");
}

Thanks&Regards
LakshmiNarayana Nalluri.

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niranjan44 on: 6/2/2011 [Member] Starter | Points: 25

Up
0
Down
hi

You can try this link

http://www.programcall.com/7/aspnet/binding-dropdownlist-with-dataset-values-in-aspnet.aspx

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 6/2/2011 [Member] Starter | Points: 25

Up
0
Down
For filling Dropdown list you can try this link..
http://vbdotnetaddict.blogspot.com/2011/05/add-and-remove-items-from-combobox-in.html

For adding data in to grid view try this link
http://vbdotnetaddict.blogspot.com/2011/02/groid-view-load-and-update-sql-data.html
Also in dropdown select index changed event call Load grid data function


vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Omniitstudent on: 2/4/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using System.Configuration;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
string cons = ConfigurationManager.ConnectionStrings["con"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();

}

}
void getdata()
{
SqlConnection con = new SqlConnection(cons);
con.Open();
SqlCommand cmd = new SqlCommand("select * from stock",con);
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sd.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "prodname";
DropDownList1.DataValueField = "prod_id";
DropDownList1.DataBind();

ListBox1.DataSource = dt;
ListBox1.DataTextField = "prodname";
ListBox1.DataValueField ="prod_id";
ListBox1.DataBind();


}
}=========
hi, try this one i think it would be help u...

omprakash

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: reddysankark-13471 on: 2/5/2012 [Member] Starter | Points: 25

Up
0
Down
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script runat="server" language="C#">
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] ddlType(string knownCategoryValues, string category)
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("function hall","function"),
new CascadingDropDownNameValue("marriage hall","marriage"),
new CascadingDropDownNameValue("party hall","party")
};
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] ddlType1(string knownCategoryValues, string category)
{
if (knownCategoryValues.Contains("function"))
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("0-250","small"),
new CascadingDropDownNameValue("250-500","large")
};
}
else if (knownCategoryValues.Contains("marriage"))
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("0-250","small"),
new CascadingDropDownNameValue("250-500","large")
};
}
else if (knownCategoryValues.Contains("party"))
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("0-250","small"),
new CascadingDropDownNameValue("250-500","large")
};
}
else
{
return null;
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] ddlType2(string knownCategoryValues, string category)
{
if (knownCategoryValues.Contains("small"))
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("10000","small")
};
}
else if (knownCategoryValues.Contains("large"))
{
return new CascadingDropDownNameValue[]
{
new CascadingDropDownNameValue("10000","large")
};
}
else
{
return null;
}
}

</script>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager" runat="Server">
</ajaxToolkit:ToolkitScriptManager>
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>

<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>


<AjaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" PromptText="select category" Category="category"
LoadingText="loading text...." ServiceMethod="ddlType">
</AjaxToolkit:CascadingDropDown>

<AjaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2" ParentControlID="DropDownList1" PromptText="select category" Category="category"
LoadingText="loading text...." ServiceMethod="ddlType1">
</AjaxToolkit:CascadingDropDown>
<AjaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3" ParentControlID="DropDownList2" PromptText="select category" Category="category"
LoadingText="loading text...." ServiceMethod="ddlType2">
</AjaxToolkit:CascadingDropDown>
</div>
</form>
</body>
</html>

sankarreddy

vijaykumarecomnets-9645, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response