1

I have the following:

    SqlConnection con = new SqlConnection(strConn);
    DataSet ds = new DataSet();
    SqlDataAdapter myda = new SqlDataAdapter("SELECT [Key], [Value] FROM [data_LookupValues] where Category = 'CanadaProvinces' ", con);
    myda.Fill(ds);
    ddlState.DataSource = ds;
    ddlState.DataTextField = "Value";
    ddlState.DataValueField = "Key";
    ddlState.DataBind(); 

I am stuck as I need to put a string value of CanadaProvinces within the SqlDataAdapter.

Is there a easy way of doing this? I get an error when I do it the way I have it.

4
  • What's the error? That code looks fine to me. Commented Nov 14, 2012 at 21:30
  • {"Incorrect syntax near '='."} Commented Nov 14, 2012 at 21:32
  • Does that query run normally in SQL Management studio (or some other query tool). Commented Nov 14, 2012 at 21:33
  • 1
    After your last edit, the query appears as correct. Do you still have the syntax error? Commented Nov 14, 2012 at 21:38

2 Answers 2

1

You haven't told us the error, however, this is no valid sql:

SELECT [Key], [Value] FROM [data_LookupValues] = 'CanadaProvinces'

What do you want to query, is data_LookupValues a table in the database?

Perhaps this is what you actually want:

SELECT [Key], [Value] FROM [data_LookupValues] WHERE Provinces = 'CanadaProvinces'

Of course i've just assumed the correct column name.

Sign up to request clarification or add additional context in comments.

Comments

0

The proper way would be to do a number of things:

  1. wrap your disposable objects into using blocks
  2. use a separate SqlCommand object and use a parametrized query (instead of concatenating together your SQL)
  3. since you're only returning a single set of data - use a DataTable instead of the much "heavier" DataSet
  4. separate the fetching of the data into its own method - and just call the from your web page - makes it universally useable, too! (will work for other things - not just CanadaProvinces)

So your code should be something like this:

private DataTable FetchData(string categoryValue)
{
   string query = "SELECT [Key], [Value] FROM [data_LookupValues] where Category = @Category";

   using (SqlConnection con = new SqlConnection(strConn))
   using (SqlCommand cmd = new SqlCommand(query, con))
   {
       cmd.Parameters.Add("@Category", SqlDbType.VarChar, 50).Value = categoryValue;

       SqlDataAdapter myda = new SqlDataAdapter(cmd);
       DataTable result = new DataTable();

       myda.Fill(result);
       return result;
    }
}

and then in your ASPX web's code-behind, use something like this:

DataTable tblCanadaProvinces = FetchData('CanadaProvinces');  

ddlState.DataSource = tblCanadaProvinces;
ddlState.DataTextField = "Value";
ddlState.DataValueField = "Key";
ddlState.DataBind(); 

and be done with it. Now you can call your FetchData method to retrieve any possible category in your lookup table!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.