VB.NET - how to retrieve values from database into dropdownlist....???

Asked By shruthi vangapally on 15-Jun-12 02:09 AM
Jitendra Faye replied to shruthi vangapally on 15-Jun-12 02:11 AM

Use this code-



SqlConnection con = new SqlConnection("con string ");
SqlDataAdapter da;
string mySQL = "Your Query";
da = new SqlDataAdapter(mySQL, con);
DataSet ds = new DataSet();
da.Fill(ds);
ddl1.DataValueField = "col1";
ddl1.DataTextField = "col2";
ddl1.DataSource=ds;

ddl1.DataBind();

Try this and let em know.

Chintan Vaghela replied to shruthi vangapally on 15-Jun-12 02:13 AM

Hi Frndz,

 

Functionality:  Bind DropDown trhogh DB table

 

 

 

Logic:

 

Public Sub BindCityDropDown(ddlCIty As DropDownList)
 
  Dim con As SqlConnection
  Dim myCMD As SqlCommand
  Dim myReader As SqlDataReader
  Try
    con = New SqlConnection(Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString")))
    con.Open()
    myCMD = New SqlCommand()
    myCMD.Connection = con
    myCMD.CommandTimeout = 200
    myCMD.CommandText = "Select * from City"
    myCMD.CommandType = CommandType.Text
    myReader = myCMD.ExecuteReader()
    ddlCIty.DataSource = myReader
    ddlCIty.DataValueField = "theName"
    ddlCIty.DataTextField = "theName"
    ddlCIty.DataBind()
    myReader.Close()
    con.Close()
  Catch ex As Exception
    Throw ex
  End Try
End Sub

 

Hope this helpful!

Thanks

 

 

shruthi vangapally replied to shruthi vangapally on 15-Jun-12 02:22 AM
eg :
if we type college name automatically thje dropdownlist which stores branch name should be updated with all the branches available in that particular college


collegename:(textbox)--->once we enter collegename all branches available in that college should appear in  the dropdownlist---
branch:(dropdownlist)
shruthi vangapally replied to Chintan Vaghela on 15-Jun-12 02:29 AM
eg :
if we type college name automatically thje dropdownlist which stores branch name should be updated with all the branches available in that particular college


collegename:(textbox)--->once we enter collegename all branches available in that college should appear in  the dropdownlist---
branch:(dropdownlist)
shruthi vangapally replied to Jitendra Faye on 15-Jun-12 02:29 AM
eg :
if we type college name automatically thje dropdownlist which stores branch name should be updated with all the branches available in that particular college


collegename:(textbox)--->once we enter collegename all branches available in that college should appear in  the dropdownlist---
branch:(dropdownlist)
Jitendra Faye replied to shruthi vangapally on 15-Jun-12 02:34 AM
For this you need to Implement TextBox_Changed event like this-

protected void txtCollege_TextChanged(object sender, EventArgs e)
    {
      SqlConnection con = new SqlConnection("con string ");
      SqlDataAdapter da;
      string mySQL = "select * from tablename where collegename='" + txtCollege.Text + "'";
      da = new SqlDataAdapter(mySQL, con);
      DataSet ds = new DataSet();
      da.Fill(ds);
      ddl1.DataValueField = "col1";
      ddl1.DataTextField = "col2";
      ddl1.DataSource = ds;
      ddl1.DataBind();

    }
Chintan Vaghela replied to shruthi vangapally on 15-Jun-12 03:07 AM

Hi Frndz,

 

Functionality:  Ontexbox Change Event Bind DropDown list trhough DB table

 

Added one texbox set AutoPostback True and added OnTextChange Event

 

<asp:TextBox ID="txtCollage"  runat="server" OnTextChanged="txtCollage_TextChanged" AutoPostBack="true"></asp:TextBox>

 

After then based on Collage Textbox value bind Dropdown

 

        myCMD.CommandText = "Select BaranchID, BranchName from Collage where CollageName='" + txtCollage.Text + "'";

 

 

Full Logic wtih Textbox and DropDown    :

 

<asp:TextBox ID="txtCollage"  runat="server" OnTextChanged="txtCollage_TextChanged" AutoPostBack="true"></asp:TextBox>

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

 

 

protected void txtCollage_TextChanged(object sender, EventArgs e)

    {

      BindBranchDDByCollageName(ddlBranch);

 

    }

    public void BindBranchDDByCollageName(DropDownList ddlBranch)

    {

 

      SqlConnection con;

      SqlCommand myCMD;

      SqlDataReader myReader;

      try

      {

        con = new SqlConnection(Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"]));

        con.Open();

        myCMD = new SqlCommand();

        myCMD.Connection = con;

        myCMD.CommandTimeout = 200;

        myCMD.CommandText = "Select BaranchID, BranchName from Collage where CollageName='" + txtCollage.Text + "'";

        myCMD.CommandType = CommandType.Text;

        myReader = myCMD.ExecuteReader();

        ddlBranch.DataSource = myReader;

        ddlBranch.DataValueField = "BranchID";

        v.DataTextField = " BranchName";

        ddlBranch.DataBind();

        myReader.Close();

        con.Close();

      }

      catch (Exception ex)

      {

        throw ex;

      }

    }

 

Hope this helpful!

Thanks

 

 

shruthi vangapally replied to Chintan Vaghela on 15-Jun-12 03:54 AM
its not working,,, after i enter data in the textbox drop down list is showing empty...this is ma code once please check it once and tell me the error--

aspx page-

<asp:DropDownList ID="DropDownList1" runat="server" Width="279px" AutoPostBack="True">

</asp:DropDownList>

aspx.vb page-

Protected Sub TextChanged(ByVal sender As Object, ByVal e As EventArgs)

BindSupplierNameByPartNumber(DropDownList1)


End Sub

Public Sub BindSupplierNameByPartNumber(ByVal DropDownList1 As DropDownList)

Dim con As SqlConnection

Dim myCMD As SqlCommand

Dim myReader As SqlDataReader

Dim strconn As String

Try

strconn = "Data Source=10.179.26.2;Initial Catalog=PPEP;user id=scala;pwd=scala"

con = New SqlConnection(strconn)

con.Open()

myCMD =

New SqlCommand()

myCMD.Connection = con

myCMD.CommandTimeout = 200

myCMD.CommandText = "select SNAME from sqm where PNUMBER='" + TextBox1.Text & "'"

myCMD.CommandType = CommandType.Text

myReader = myCMD.ExecuteReader()

DropDownList1.DataSource = myReader

DropDownList1.DataTextField = "SNAME"

DropDownList1.DataBind()

myReader.Close()

con.Close()

Catch ex As Exception

Throw ex

End Try

End Sub

dipa ahuja replied to shruthi vangapally on 15-Jun-12 04:44 AM
void bindDropDonw()
{
  SqlDataAdapter da = new SqlDataAdapter("select empid from emp", "ConnectionString");
 
  DataTable dt = new DataTable();
  da.Fill(dt); // Fill the dataset
 
  DropDownList1.DataSource = dt;
  DropDownList1.DataValueField = "empid";
  DropDownList1.DataTextField = "empid";
  DropDownList1.DataBind();
}
 
Chintan Vaghela replied to shruthi vangapally on 15-Jun-12 06:11 AM

Hi Frndz,

 

Functionality:  DropDown  Bind Problem

 

First Pass DataValue Field as Same as DataTextField

 

DropDownList1.DataTextField = "SNAME"

DropDownList1.DataValueField = "SNAME"

 

 

After then check in sqm table has data or not for search PNUMBER

 

 

Logic   :

 

DropDownList1.DataSource = myReader

DropDownList1.DataTextField = "SNAME"

DropDownList1.DataValueField = "SNAME"

DropDownList1.DataBind()

 

Hope this helpful!

Thanks

 

 

 

shruthi vangapally replied to Chintan Vaghela on 18-Jun-12 12:04 AM
thankyou so much!! i have got it:):)
shruthi vangapally replied to Jitendra Faye on 18-Jun-12 12:05 AM
thankyou!! i got it:):)
Jitendra Faye replied to shruthi vangapally on 18-Jun-12 12:48 AM
You always welcome.