0

I'm actually storing a value using session and then redirecting to other page using image button where based on the session value I'm fetching the Data from database. I'm getting an user defined exception in the line. please help!

adap.Fill(dt1);//[SqlException (0x80131904): Incorrect syntax near '='.]

And this my coding.

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    ImageButton btn = sender as ImageButton;
    string modelId = btn.CommandArgument;
    Session["modelid"] = modelId;
    Response.Redirect("details.aspx");
} 

public partial class details : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=VISH;Initial Catalog=VISH;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
        SqlDataAdapter adap = new SqlDataAdapter("select * from details1 where Modelid=" + Session["modelid"], con);
        DataTable dt1 = new DataTable();
        adap.Fill(dt1);
        DataList1.DataSource = dt1;
        DataBind();
    }
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
sharanya
  • 15
  • 1
  • 2
  • 9

2 Answers2

0

Cast the session object to a string first.

(String) Session["modelid"] 
Pleun
  • 8,856
  • 2
  • 30
  • 50
  • where should I do that? in the page where I'm declaring session or in the page where I'm using session for selecting data from database? – sharanya Mar 25 '15 at 17:02
0

I recommend you to avoid using string concatenation for command creation - use parameters instead.

Also, you have to check whether Session["modelid"] has value or not, because parameter with null value will cause exactly the given message:

The parameterized query '(@id nvarchar(4000))select * from details1 where Modelid=@id' expects the parameter '@id', which was not supplied

So, the code will be:

protected void Page_Load(object sender, EventArgs e)
{
    con.Open();

    var id = Session["modelid"];

    if (id != null)
    {        
        SqlDataAdapter adap = new SqlDataAdapter("select * from details1 where Modelid=@id" , con);
        adap.SelectCommand.Parameters.AddWithValue("@id", );

        DataTable dt1 = new DataTable();
        adap.Fill(dt1);
        DataList1.DataSource = dt1;
        DataBind();
    }
}

Or use the Page.IsPostBack property if it is guaranteed that Session["modelid"] will be set after the first postback.

Or just use some default value for it.

P.S.: Also, it is not a good idea to use instance level connection where you can't Dispose (Close) it in a deterministic way. It is better to make it method level variable and use it with using clause:

protected void Page_Load(object sender, EventArgs e)
{
     using (SqlConnection con = new SqlConnection("Data Source=VISH;Initial Catalog=VISH;Integrated Security=True"))
     {
         // ...        
     }
}
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • now this is the exception The parameterized query '(@id nvarchar(4000))select * from details1 where Modelid=@id' expects the parameter '@id', which was not supplied. – sharanya Mar 25 '15 at 17:09
  • @sharanya Hmm, It is actually provided - `AddWithValue("@id", Session["modelid"]);`. Strange, I'll check - perhaps `SqlDataAdapter` has some problems with parametrized queries. – Eugene Podskal Mar 25 '15 at 17:13
  • since I'm using datalist in both d pages I'm fetching it from database. this is the designing part of imagebutton in datalist and the coding part i have mention above can u please tell me what could be the possible mistake in this ? – – sharanya Mar 25 '15 at 17:15
  • @sharanya Well, I have tried and SqlDataAdapter has no problem with commands that have parameters. It seems that the problem is with `Session["modelid"]`. What is its value and what is the type of the `Modelid` column? – Eugene Podskal Mar 25 '15 at 17:34
  • as i mentioned in the previous comment I'm fetching the modelid from database and I'm using image button to redirect to other page which ll show more details about the image which is clicked. To do that I used DataList in both pages. In the 1st page It is taking the values from the database and displaying there's no problem with it. But, for displaying the particular details I used CommandArgument='<%# Eval("modelid") in the ImageButton And Coding of the imagebutton is above . – sharanya Mar 25 '15 at 17:51
  • @sharanya It seems that `Session["modelid"]` had null value. I have modified the answer. – Eugene Podskal Mar 25 '15 at 18:22
  • yes... @eugene you're right... my session is null.. can You tell me what went wrong in my coding..protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { ImageButton btn = sender as ImageButton; string modelId = btn.CommandArgument; Session["modelid"] = modelId; Response.Redirect("details.aspx"); } – sharanya Mar 26 '15 at 12:32
  • @sharanya You set `Session["modelid"]` on some button click, but on the first load (or if that button is not clicked) it is null. There is nothing really wrong - you just have to avoid any load operations if it is null or initially set it to some default value. – Eugene Podskal Mar 26 '15 at 12:35
  • but actually I'm using it in image button.. Only after clicking the image button it is redirecting to the other page.. I don't understand how could it be null? – sharanya Mar 26 '15 at 12:46
  • @sharanya Because Page_Load is executed before any specific event handlers - https://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx. – Eugene Podskal Mar 26 '15 at 12:52