Mahbube
Mahbube

Reputation: 13

C# get result of Select using DataAdapter and Dataset

I want to execute a SELECT query but it throws an exception:

No mapping exists from object type System.Web.UI.HtmlControls.HtmlSelect to a known managed provider native type.

this is my code:

        DateTime start = new DateTime(startY, startM, startD);
        //string s = start.ToString();
        DateTime end = new DateTime(endY, endM, endD);
        string title = title_key.Value;
        string cat = sCategory.Value;

        var sqlcon = new SqlConnection("Data Source=maryam-pc;Initial Catalog=news_portal1;Integrated Security=True;");
        sqlcon.Open();

        SqlCommand sqlcom = new SqlCommand();
        sqlcom.Connection = sqlcon;
       // SqlCommandBuilder sc = new SqlCommandBuilder();
        //sc.

       sqlcom.CommandText = "select title,text,category,datetime from news where  category= @cat and title = @ttl ";
       // sqlcom.CommandText = "select pk,title,text,category,datetime from news ";
        sqlcom.Parameters.AddWithValue("@ttl", title);
        sqlcom.Parameters.AddWithValue("@cat", sCategory);
        sqlcom.Parameters.AddWithValue("@start", start.ToString());
        sqlcom.Parameters.AddWithValue("@end", end.ToString());
        // sqlcom.Parameters.AddWithValue("@ttl", title);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = sqlcom;
        //GridView GridView1 = new GridView();
        da.Fill(ds);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count != 0)
            {
                System.Diagnostics.Debug.WriteLine("not empty");
                SearchResults.DataSource = ds;
                SearchResults.DataBind();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("empty");
                SearchResults.DataSource = null;
                SearchResults.DataBind();
            }
        }



    }

title, category are varchar() and the dates are dateTime.

Upvotes: 0

Views: 1467

Answers (1)

Josh K
Josh K

Reputation: 765

Use the value of the select instead of the select itself:

sqlcom.Parameters.AddWithValue("@cat", sCategory.Value);

Or, use the variable that you declared and initialized earlier in the code:

sqlcom.Parameters.AddWithValue("@cat", cat);

Upvotes: 1

Related Questions