Question

I have a DropDownList bounded with List of Items from the SqlDatasource. The List of Items in the DropDownList are bounded based on the following query

SELECT COLUMN_NAMES FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT')

My Requirement: How to assign Multiple Datatypes for those DropDownList Items i.e. Few indexes in the List of Items shall be grouped as Decimal Datatype and Few Indexes shall be grouped as Varchar , DateTime Dataypes.

I would like to generate Dynamic Web controls based on the Datatype of DDL input and the correspoinding DropDownList_SelectedIndexChanged(). My SQL query:

SELECT COLUMN_NAME, DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='RESULT' group by DATA_TYPE,COLUMN_NAME

Output:

COLUMN_NAME  DATA_TYPE  

a            decimal
b            decimal
c            decimal
d            int
e            int
f            varchar
g            varchar
h            varchar
i            varchar
j            varchar

If a or b or c are selected from DDL i need to generate dynamic web controls uniquely for Decimal datatype SelectedIndexValue.

How to Proceed?

My aspx code:

<asp:Panel ID="Panel5" runat="server" Height="221px">
        <span style="font-size: 135%; font-family: Verdana; font-weight: bold"> Search Functionalities </span>
        <asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="All_Columns" DataValueField="All_Columns" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="true">
        </asp:DropDownList>
        <asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME 'All_Columns' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='RESULT'  "></asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Small" OnClick="Button1_Click" Text="Search Flow Periods" Width="144px" />
        <asp:Table ID="dynamic_filter_table" runat="server" ToolTip="Results">
        </asp:Table>
</asp:Panel>

C# code:

  protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
     {
         if(DropDownList5.SelectedIndex.Equals(typeof(Decimal)))
         {
            createdynamiccontrols();
         }
         else if (DropDownList5.SelectedIndex.Equals(typeof(DateTime)))
         {
             //Create Unique Web Controls
         }

         else if (DropDownList5.SelectedIndex.Equals(typeof(Char)))
         {
             //Create Unique Web Controls
         }
     }
Was it helpful?

Solution

Equals method will do object level comparison. Which means it will check if both objects are same or not. In your case DropDownList5.SelectedIndex will give int object, which you are comparing with typeof other object, which will always return false, unless the typeof is int.

Since you are getting the result with the column names as COLUMN_NAME and DATA_TYPE, change your DropDownList tag to include these column names. As,

<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="COLUMN_NAME" DataValueField="DATA_TYPE" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged"  AutoPostBack="true">
        </asp:DropDownList>

Then you will get the DataType of the column in DropDownList5.SelectedValue, which is a string. Then you can add the controls based on the DropDownList selection by comparing SelectedValue with the string name of the DataType. As,

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
    if(DropDownList5.SelectedValue == "decimal")
    {
        TextBox1.Text = dr.GetString(0);
    }
    else if(DropDownList5.SelectedValue == "int")
    { 
        Label1.Text = dr.GetString(0);
    }
    else if(DropDownList5.SelectedValue == "varchar")
    {
        Button1.Text = dr.GetString(0);
    }
}

OTHER TIPS

I'm assuming your scenario in this way, You need to categorize the column_name values based on datatype, once categorized the selected values should display in unique web controls right ?. If so, try with this line of code in dropdwon selectchange event.

SqlConnection con=new Sqlconnenction("Your Connection String");
con.Open();
Sqlcommand cmd = new SqlCommand("Select * from your database",con); // Your two columns
SqlDateReader dr=cmd.ExecuteReader();
while(dr.Read())
{
  if(dr.GetString(1) == "decimal") // dr.GetString(1) = 'Your Second Column'
  {
    TextBox1.Text = dr.GetString(0);
  }
  else if(dr.GetString(1)=="int")
  {
    Label1.Text = dr.GetString(0);
  }
  else if(dr.GetString(1)=="varchar")
  {
    Button1.Text = dr.GetString(0);
  }
}
con.Close();

Let me know if i confused you.

Correct Solution :

If "e" is chosen from The following DropDownList inputs the value retained in DropDownList is "e"

COLUMN_NAME  DATA_TYPE  
a            decimal
b            decimal
c            decimal
d            int
e            int
f            varchar
g            varchar  
h            varchar
i            varchar
j            varchar

Source: DropDownList with repeated data value fields OnSelected get confused and chooses the first one why?

Aspx Code:

<asp:DropDownList ID="DropDownList5" runat="server"  AutoPostBack="true" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" DataSourceID="column_list_for_filter">
</asp:DropDownList>                           

<asp:SqlDataSource ID="column_list_for_filter" runat="server">
</asp:SqlDataSource>

C# code:

protected void Page_Load(object sender, EventArgs e)
    {      
        if (!IsPostBack)
        {
            BindDropDownLists();
        }

        else
        {
            if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
            {
                if (DropDownList5.SelectedValue.Contains("decimal"))
                {
                    createdynamiccontrols_decimal();
                }

                else if (DropDownList5.SelectedValue.Contains("varchar"))
                {
                    createdynamiccontrols_varchar();
                }
                else if (DropDownList5.SelectedValue.Contains("datetime"))
                {
                    create_cntrls();
                }

                else if (DropDownList5.SelectedValue.Contains("int"))
                {
                    Create();
                }


            }

        }
    }

private void BindDropDownLists()
     {
         column_list_for_filter.ConnectionString = connection;
         column_list_for_filter.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'RESULT' AND COLUMN_NAME IN ('a','b','c','d','e','f','g','h','i'))";
         DropDownList5.DataTextField = "COLUMN_NAME";
         DropDownList5.DataValueField = "DATA_TYPE";
         DropDownList5.DataBind();

     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top