0

I did this method to return a dropdownlist, it returns it fine and everything, but in the dropdownlist it doesn't show me the values. When I do a debug, the dropdownlist to which I am assigning what the method returns has the correct values, but the dropdownlist appears empty

This is the method

public DropDownList DropDown(string sqlstring, string dValue, string dField)
{
   DropDownList DDLBox = new DropDownList();
   cmd = new MySqlCommand(sqlstring, con);
   cmd.CommandType = CommandType.StoredProcedure;
   dt = new DataTable();
   da = new MySqlDataAdapter(cmd);
   da.Fill(dt);
   DDLBox.DataSource = dt;
   DDLBox.DataTextField = dField;
   DDLBox.DataValueField = dValue;
   DDLBox.DataBind();
   return DDLBox;
}

This the call

protected void Page_Load (object sender, EventArgs e)
{
...
DBAplication =  BD.DropDown("sp_Aplication", "Code", "Name");
DBAplication.Items.Insert(0, new ListItem(""));
...
}

I want to solve this problem that I have, so I can better organize my code

  • Please check the method signature of DropDown. Assuming this is C# code, string is a datatype and you are declaring a string of type string. This should throw an error. – SoftwareDveloper Apr 04 '23 at 15:59
  • My mistake, I already changed it, but the problem continues – Heinz Garcia Apr 04 '23 at 16:41
  • Incidentally, you should *not* prefix your stored procedure names with "sp_": [Avoid Naming User Stored Procedures SP% or SP_%](https://stackoverflow.com/questions/20530211/avoid-naming-user-stored-procedures-sp-or-sp). – Andrew Morton Apr 04 '23 at 16:42

1 Answers1

0

Ok, a good idea, but you have a BOATLOAD of issues here.

First up by creating a ddl in code, then when you "assign" that to the existing control on the web page, you are BLOWING OUT a HUGE amount of settings and setup. Remember, when you drop in that control:

The "designer" file adds that ddl to the page class.

upon running, then a BOATLOAD of settings occur including that of view state, and a WHOLE bunch of connections setup to the current page class. So, all kinds of things will occur. Including you "blowing" out the events that you no doubt would want to have for the ddl (such as SelectedIndexChanged). So, what happens when you add/change events, and other settings to that ddl? Your code going to blow out all those settings - including that of having "view state" attached to the control to operate correctly on that page. So "too many" properties you would have to setup for this to work.

There are two solutions I suggest:

Create a user control, and then simple have some properties and methods - includes stored procedure name. That way, you can just drag + drop from the project explore the control, and you are done.

However, somewhat FAR more simple?

Just pass the current instance of the ddl object to your helper routine.

so, say I have this markup:

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

Then this code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropHelper(DropDownList1,
            "SELECT id, HotelName FROM tblHotelsA",
            "id","hotelname",
            "Select a Hotel");

    }
}


void DropHelper(DropDownList dl, string strSQL, 
                string dValue, string dField,
                string sFirstLine)
{

    dl.DataValueField = dValue;
    dl.DataTextField = dField;

    using (SqlConnection con = new SqlConnection(Properties.Settings.Default.TEST4))
    {
        using (SqlCommand cmd = new SqlCommand(strSQL, con))
        {
            DataTable dt = new DataTable();
            con.Open();
            dt.Load(cmd.ExecuteReader());
            dl.DataSource= dt;
            dl.DataBind();
            dl.Items.Insert(0, new ListItem(sFirstLine, ""));
        }
    }
}

So, "pass" the existing control in question. That way its attachment to the current page class object, the viewstate (and a boatload of other settings you would have to code out) are not required to be setup by you. So, creating a whole new instance of that control means you lose all of the code behind settings that visual studio create(s) and sets up for you. (including things like viewstate etc.).

And now we see/get this:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thanks for the explanation, I'm new to the world of C# and asp.net and I'm making a thousand inventions to learn – Heinz Garcia Apr 04 '23 at 17:17
  • Your goal of "helper" routines is a good road to take. However, as noted, attempting to do what the designer(s) do in code? Quite a challenge - and besides, as above shows you have lots of options anyway. Of course in above, I placed the code in the page (code behind), and you certainly could/would/can/should move that helper routine to your overall project in say a "general" code class - that class will of course be static, and thus just keep in mind to NOT use class level variables in that static class, since all such variables would be shared by all of users of the web site!!! – Albert D. Kallal Apr 04 '23 at 17:22
  • and as a curtesy? Do mark this as a answer. – Albert D. Kallal Apr 04 '23 at 17:23