0

This code is throwing an error on the line marked below, can anyone point me in the right direction to why this is throwing an error?

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
    SqlDataAdapter dadapter;
    DataSet dset; 
    string sql = "SELECT * from  SocoetyMaintan";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dadapter = new SqlDataAdapter(sql, con);
            dset = new DataSet();
            dadapter.Fill(dset);
            DropDownList1.DataSource = dset.Tables[0];
            DropDownList1.DataTextField = "FullName";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
            GridViewBind();
        }
    }
      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewBind();
        }
        public void GridViewBind()
        {
            dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id=" + DropDownList1.SelectedValue + "", con);
            dset = new DataSet(); //Throwing Error Here
            dadapter.Fill(dset);
            GridView1.DataSource = dset.Tables[0];
            GridView1.DataBind();
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • 2
    Use prepared statements. What is the type of the `Id` column in your table, and what C# type are you trying to bind to it? – Tim Biegeleisen Oct 03 '18 at 05:49
  • When you say error - do you mean compilation time error? – Prateek Shrivastava Oct 03 '18 at 05:52
  • Error simply means that Select statement format is incorrect, it cannot be parsed, why can't you just figure out the Select statement generated and whether it would run as plain sql or not, that's the whole issue – Mrinal Kamboj Oct 03 '18 at 05:52
  • try this => `dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id='" + DropDownList1.SelectedValue + "'", con);` – er-sho Oct 03 '18 at 05:54
  • Did you open the SQL connection anywhere in your code? – Vlam Oct 03 '18 at 05:55
  • No, where exactly i need to open that? –  Oct 03 '18 at 06:05
  • @Poojakulkarni, did u try my above commented code? – er-sho Oct 03 '18 at 06:08
  • yes i tried this as well it says "The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'GridDataSource' could not be found" –  Oct 03 '18 at 06:15
  • try to set your `asp:GridView` => `DataSourceID="GridDataSource"` and `asp:SqlDataSource` => `ID="GridDataSource"` and keep my previous commented code as it is and let me know. – er-sho Oct 03 '18 at 06:20
  • Its working !!!! thank you!! @ershoaib –  Oct 03 '18 at 06:35
  • so can i add this as answer to yuor question? then u just mark tick beside the answer – er-sho Oct 03 '18 at 06:36
  • yes you can! surely i will tick, as you know i am new here...just let me know where exactly –  Oct 03 '18 at 06:38
  • @Poojakulkarni, I added my answer, you just seen tickmark at left side of answer :) you just need to tick to make it green – er-sho Oct 03 '18 at 06:44

4 Answers4

0

Here, you forgot to convert DropDownList1.SelectedValue to string or int whatever in your database. So do it as follow. For example,

dadapter = new SqlDataAdapter("select * from SocoetyMaintan where Id='" + DropDownList1.SelectedValue.ToString() + "';", con);
ksdev
  • 56
  • 7
0

It think you're missing quotes in Id=.

 $"SELECT * from SocoetyMaintan where Id='{DropDownList1.SelectedValue}';"

BTW. You should never allow direct input from user into your queries. Look up 'SQL injection'.

tymtam
  • 31,798
  • 8
  • 86
  • 126
0

The way you are writing may give error. Try the following

Method 1:

SqlDataAdapter dadapter = new SqlDataAdapter(string.Format("SELECT * FROM SocoetyMaintan WHERE Id = '{0}'",DropDownList1.SelectedValue), con);

Method 2: (Preferred)

using (SqlDataAdapter dadapter = new SqlDataAdapter("SELECT * FROM SocoetyMaintan WHERE Id = @id", con))
{
    int filter = ID;
    dadapter.SelectCommand.Parameters.AddWithValue("@id", DropDownList1.SelectedValue);
}
ManishM
  • 583
  • 5
  • 7
0

1) Just try to change your query like below

dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id='" + DropDownList1.SelectedValue + "'", con);

Note: Better to use prepared statements to your query.

dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id=@Id", con);
dadapter.SelectCommand.Parameters.AddWithValue("@Id", DropDownList1.SelectedValue);

2) Try to set your asp:GridView => DataSourceID="GridDataSource" and asp:SqlDataSource => ID="GridDataSource"

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • @Poojakulkarni, view the answer might be it help you :) – er-sho Oct 03 '18 at 06:42
  • Hello Sir, need one more help , in the above code I want to add iff statement for GridViewBind() where in the else case i want to say "NO DATA TO DISPLAY" so how to write such if statement condition? @ershoiab –  Oct 04 '18 at 04:48
  • check this https://stackoverflow.com/questions/2976473/how-to-test-if-a-dataset-is-empty – er-sho Oct 04 '18 at 05:51