Click here to Skip to main content
16,012,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to dynamically fill the dropdown list depends on the function call in c#

What I have tried:

C#
public static string Bindyears(string type)
    {
        string s = type;
        string ConfigString = ConfigurationManager.ConnectionStrings["autobuyConnectionString"].ConnectionString;
        StringWriter builder = new StringWriter();
        String strQuery = "select distinct  car_year  , car_year as car_year1 from  s where car_year !='0' and car_year!=''    order by car_year desc ";
        
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection(ConfigString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                
                cmd.Connection = con;
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
        }
        DataTable dt = ds.Tables[0];
        builder.WriteLine("[");
        if (dt.Rows.Count > 0)
        {
            // builder.WriteLine("{\"optionDisplay\":\"Select Make\",");
            // builder.WriteLine("\"optionValue\":\"0\"},");
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["car_year"] + "\",");
                builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["car_year1"] + "\"},");
            }
        }
        else
        {
            builder.WriteLine("{\"optionDisplay\":\"Select Year\",");
            builder.WriteLine("\"optionValue\":\"0\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }
 public static string Bindyears(string type)
    {
        string s = type;
        string ConfigString = ConfigurationManager.ConnectionStrings["autobuyConnectionString"].ConnectionString;
        StringWriter builder = new StringWriter();
        String strQuery = "select distinct  car_year  , car_year as car_year1 from  where car_year !='0' and car_year!=''    order by car_year desc ";
        
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection(ConfigString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strQuery;
                
                cmd.Connection = con;
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }
        }
        DataTable dt = ds.Tables[0];
        builder.WriteLine("[");
        if (dt.Rows.Count > 0)
        {
            // builder.WriteLine("{\"optionDisplay\":\"Select Make\",");
            // builder.WriteLine("\"optionValue\":\"0\"},");
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["car_year"] + "\",");
                builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["car_year1"] + "\"},");
            }
        }
        else
        {
            builder.WriteLine("{\"optionDisplay\":\"Select Year\",");
            builder.WriteLine("\"optionValue\":\"0\"},");
        }
        string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
        returnjson = returnjson + "]";
        return returnjson.Replace("\r", "").Replace("\n", "");
    }
Posted
Updated 26-Apr-16 0:38am
v2

do little modification as

C#
public static string Bindyears(string type)
   {
       string s = type;
       string ConfigString = ConfigurationManager.ConnectionStrings["autobuyConnectionString"].ConnectionString;
       StringWriter builder = new StringWriter();
       String strQuery = "select distinct  car_year  , car_year as car_year1 from  @type where car_year !='0' and car_year!=''    order by car_year desc ";

       DataSet ds = new DataSet();
       using (SqlConnection con = new SqlConnection(ConfigString))
       {
           using (SqlCommand cmd = new SqlCommand())
           {
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = strQuery;
               cmd.Parameters.Add("@type",type);
 
Share this answer
 
Comments
Nethaji chennai 26-Apr-16 7:19am    
thankyou
Karthik_Mahalingam 26-Apr-16 7:21am    
pls close the post, if it is resolved
mannaggialamadonna 19-Jun-19 9:29am    
It doesn't work
You can't - the table name has to be available at SQL "compile time" and can't be a variable.
There are ways around this: you can build a NVARCHAR command in SQL and use EXEC to execute it, or you can concatenate strings to produce the SQL command you send from your C# code.

But...and it's a big "but"...both are dangerous and have to be done with extreme caution. Concatenating strings is dangerous - it exposes your code to SQL Injection, so you have to be very, very aware of exactly what can get assembled into the string that gets sent to SQL if you want to keep your database intact! If your users can provide the text at all, you are at a very real risk of damage or deletion if they (for example) enter
cars; DROP TABLE cars;--
and you concatenate that into your string or NVARCHAR value.

Personally? I wouldn't do it: I'd use fixed strings and select the appropriate one from a list instead of building them in any way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900