-4

I have a form in which I have a ComboBox control with two values.

Once the user selects one item in the list, I need to display a table of data corresponding to the option selected.

if (DropDownList1.SelectedValue == 1) 
{ 
    // open a SqlCommand
    // read values 
    // fill table
}
else if (DropDownList1.SelectedValue == 2)     
{ 
     // etc

The problem is that I only want to fill the table after the user makes a selection. But when the form is loaded the ComboBox automatically chooses the first option and my code runs, filling the table.

How do I prevent this from happening?

Girish
  • 306
  • 3
  • 17
  • 3
    You've asked for ideas but what are you having problems with? What have you tried already? – zeencat Aug 06 '12 at 08:06
  • Deleting questions can result in an automatic question ban. It is always better to improve existing ones rather than to delete them. Also, I hope my edit represents your actual question. –  Aug 16 '12 at 13:45

2 Answers2

1

what did you try so far?

there is no problem in having the table name coming from a DropDown, it's more about understanding and designing the Data Access layer for your application. Do you have a save button in your WebForm and are you composing the SQL code dynamically in the code behind or using stored procedures?

starting point to connect to SQL Server and execute a command is generally similar to the following (if you do not use ORM like Entity Framework):

using (varconn = new SqlConnection(connectionString)) 
using (var cmd = conn.CreateCommand(...)))
{ 
    conn.Open();

    cmd.ExecuteNonQuery();
}

if you need more ideas about how to use EF (or simply a DAL technique) with ASP.NET (MVC or Web Forms), see my answer here: https://stackoverflow.com/a/7474357/559144

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Hi..Thanks for the response.. Well.. I have not tried with anything so far, only because I was clueless how to go about it. I know ADO.NET and connections with my DB.. But I have a form in which, if a user selects an item from that list, suppose, if he selects item called "Confirmend Order", then all the values in the form must be inserted into `dbo.Confirmend_order`. So this is when I'm getting stuck.. – Girish Aug 06 '12 at 08:27
  • 1
    yes, so you will have to create some methods in some DataAccessLayer class library where you can pass all those values from the UI controls as parameters and then you will use either stored procedure with parameters or inline/dynamyc SQL creation and call the ExecuteNonQuery on the command object like in my snippet above, makes sense? – Davide Piras Aug 06 '12 at 08:30
  • Thank you but I have no idea why I'm downvoted 2 times.. Anyways thanks, – Girish Aug 06 '12 at 08:40
  • 1
    I voted you up to remove the downvotes but can do it only once. People here do not like vague questions without an example of what you have tried so far. – Davide Piras Aug 06 '12 at 08:41
  • 1
    @Girish: Yes, Davide is correct!!! I think you should read this bit **http://stackoverflow.com/faq#questions** and edit your question accordingly :) – huMpty duMpty Aug 06 '12 at 08:42
0
        DataSet ds = new DataSet();
        ds.Tables.Add(new DataTable("DataTableNo1"));
        ds.Tables.Add(new DataTable("DataTableNo2"));

        string dtname = comboBox1.Text;
        ds.Tables[dtname].Rows.Add(new DataRow());
VoonArt
  • 884
  • 1
  • 7
  • 21