Skip to content
Loading
C# How To Display "Tables" in DB at the Drop Down List Box
  • Raja T
    Hi Rajveer, Please share your aspx page code also..y means here "DropDownList.SelectedIndex" It is worng i think Please check DropDownListId in aspx page...Please share to me your aspx page...
  • hi im trying Supreeth code because i dont really understand your code.. sorry im a beginner in programming i hope u will help and guide me. thank you. above is the recent error i have
  • protected void Button2_Click(object sender, EventArgs e)
    {
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Project\WebApplication2\App_Data\Database1.mdf;Integrated Security=True");
    if (DropDownList.SelectedIndex == 1) //Choose --Select--
    {
    }
    else if (DropDownList.SelectedIndex == 2) //Choose Dog
    {
    SqlCommand cmd = new SqlCommand("Insert into dog(Id, Breed, Name, Age, Gender, Weight, Date) Values (@Id, @Breed, @Name, @Age, @Gender, @Weight, @Date)", con);
    cmd.Parameters.AddWithValue("@Id", tb7.Text);
    cmd.Parameters.AddWithValue("@Breed", tb1.Text);
    cmd.Parameters.AddWithValue("@Name", tb2.Text);
    cmd.Parameters.AddWithValue("@Age", tb3.Text);
    cmd.Parameters.AddWithValue("@Gender", tb4.Text);
    cmd.Parameters.AddWithValue("@Weight", tb5.Text);
    cmd.Parameters.AddWithValue("@Date", tb6.Text);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }
    else if (DropDownList.SelectedIndex == 3) //Choose Cat
    {
    SqlCommand cmd = new SqlCommand("Insert into cat(Id, Breed, Name, Age, Gender, Weight, Date) Values (@Id, @Breed, @Name, @Age, @Gender, @Weight, @Date)", con);
    cmd.Parameters.AddWithValue("@Id", tb7.Text);
    cmd.Parameters.AddWithValue("@Breed", tb1.Text);
    cmd.Parameters.AddWithValue("@Name", tb2.Text);
    cmd.Parameters.AddWithValue("@Age", tb3.Text);
    cmd.Parameters.AddWithValue("@Gender", tb4.Text);
    cmd.Parameters.AddWithValue("@Weight", tb5.Text);
    cmd.Parameters.AddWithValue("@Date", tb6.Text);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    }
    if (IsPostBack)
    {
    tb1.Text = "";
    tb2.Text = "";
    tb3.Text = "";
    tb4.Text = "";
    tb5.Text = "";
    tb6.Text = "";
    }
  • hello ive been working so hard but i still have a problem now.. this is my code: protected void Button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Project\WebApplication2\App_Data\Database1.mdf;Integrated Security=True"); if (DropDownList.SelectedIndex == 1) //Choose --Select-- { Label8.Text = "Choose any One"; } else if (DropDownList.SelectedIndex == 2) //Choose Dog { SqlCommand cmd = new SqlCommand("Insert into dog(Id, Breed, Name, Age, Gender, Weight, Date) Values (@Id, @Breed, @Name, @Age, @Gender, @Weight, @Date)", con); cmd.Parameters.AddWithValue("@Id", tb7.Text); cmd.Parameters.AddWithValue("@Breed", tb1.Text); cmd.Parameters.AddWithValue("@Name", tb2.Text); cmd.Parameters.AddWithValue("@Age", tb3.Text); cmd.Parameters.AddWithValue("@Gender", tb4.Text); cmd.Parameters.AddWithValue("@Weight", tb5.Text); cmd.Parameters.AddWithValue("@Date", tb6.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } else if (DropDownList.SelectedIndex == 3) //Choose Cat { SqlCommand cmd = new SqlCommand("Insert into cat(Id, Breed, Name, Age, Gender, Weight, Date) Values (@Id, @Breed, @Name, @Age, @Gender, @Weight, @Date)", con); cmd.Parameters.AddWithValue("@Id", tb7.Text); cmd.Parameters.AddWithValue("@Breed", tb1.Text); cmd.Parameters.AddWithValue("@Name", tb2.Text); cmd.Parameters.AddWithValue("@Age", tb3.Text); cmd.Parameters.AddWithValue("@Gender", tb4.Text); cmd.Parameters.AddWithValue("@Weight", tb5.Text); cmd.Parameters.AddWithValue("@Date", tb6.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } if (IsPostBack) { tb1.Text = ""; tb2.Text = ""; tb3.Text = ""; tb4.Text = ""; tb5.Text = ""; tb6.Text = ""; } When i run the application it works, but when i choose Cat from the drop down list and i add the details.. the details does not go into the Cat tables but it stores in the Dog table.. why is this happening??
  • Raja T
    Hi, Please check out below url for above error
     
     
    http://stackoverflow.com/questions/2962927/a-transport-level-error-has-occurred-when-receiving-results-from-the-server 
  • Raja T
    Hi Rajveer, Which code are you tried  Supreeth J or my code?
  • Hey now im having this error "A transport-level error has occurred when receiving results from the server. (provider: Named Pipes Provider, error: 0 - The handle is invalid.)" i tried troubleshooting but i cant
  • Raja T
    Hi,
     

    onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="130px" AutoPostBack="true">
    Dog
    Cat
     
  • Supreeth J
    Hi,
    In  HTML code write,
     
    DropDownList" runat="server">
     
     
     DropDownList
     same as be put in 
     
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
    if (DropDownList.SelectedIndex == 1) //Choose --Select--
    {
    label.text="Choose any One";
    }
    else if (DropDownList.SelectedIndex == 2) //Choose Dog
    {
    query = "insert into dog values(........)";
    }
    else if (DropDownList.SelectedIndex == 3) //Choose Cat
    {
    query = "insert into cat values(........)";
    }
    }
     
  • Raja T
    Hi, try like
     
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    string table_name = DropDownList1.SelectedItem.Value;
    //or    table_name = DropDownList1.SelectedItem.Text;
    string insertquery = "insert into " + table_name + " values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Value + "')";
    SqlConnection con = new SqlConnection("");
    SqlCommand cmd = new SqlCommand(insertquery, con);
    con.Open();
    int i = Convert.ToInt32(cmd.ExecuteNonQuery());
    con.Close();
    if (i > 0)
    {
    Label3.Text = "Record Inserted Successfully";
    }
    }​
     
  • i have one problem, when i run the program the list doesnt show me either Dog or Cat. how do i put that into the drop down list box?
  • okay i will try it on my program. if i have any errors, i will get back to you! thank you so much for your help
  • Supreeth J
    Hi Rajveer,
     
    It Will work fine, i did 1 of the Project on it 
  • Hello thanks for helping, but this was not what i was looking for.. because the code in your website shows differenr drop down list box for different databse tables... But what im looking for is all my databse tables in ONE drop down list box.
  • Thank You Supreeth. Just note im developing in C# using ASP.net. and im doing a web not a application. does the code u helped me with still works?
  • Raja T
    Below link might hepls you(Cascading) http://www.aspdotnet-suresh.com/2010/10/how-to-populate-dropdown-based-on-other.html
  • Raja T
    Hi Rajveer, Try like on SelectedIndexChanged event get the selected value (selected text ex dog/cat) and load drop downlist based on selected value...
  • Supreeth J
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       if (DropDownList.SelectedIndex == 1) //Choose --Select--
          {
             label.text="Choose any One";
          }
      else if (DropDownList.SelectedIndex == 2) //Choose Dog
          {
             query = "insert into dog values(........)";
          }
     
      else if (DropDownList.SelectedIndex == 3) //Choose Cat
       {
          query = "insert into cat values(........)";
       }