Click here to Skip to main content
15,884,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Please solve this error.
my code is
C#
con.Open();
        using (SqlCommand cmd = new SqlCommand("select ID from Vendor_gatepass where GatePass_Status='Close' and Vendor_GatePass='" + Gatepass.Text + "'", con))
        {
            SqlDataReader dr1 = cmd.ExecuteReader();
            List<int> list = new List<int>();
            if (dr1.HasRows)
            {
                while (dr1.Read())
                {
                    list.Add(Convert.ToInt32(dr1[0]));
                }
            }
            Session["ID"] = list;

            dr1.Close();


            string query = "update Product_Details set [GatePass status]='Close' where ID in ('"+Session["ID"]+"')";
            SqlCommand comdd = new SqlCommand(query, con);
            comdd.ExecuteNonQuery();
            con.Close();
    }}
Posted
Comments
Arjsrya 6-Jan-15 4:56am    
What error are you getting exactly?Did you check the value while debugging in the while loop?Please check Null before adding it into the List.

Have you debug your code and check what the value is coming in dr1[0], you got the error from there.

and also your query

SQL
string query = "update Product_Details set [GatePass status]='Close' where ID in ('"+Session["ID"]+"')";


I think you need to update data with using IN cause, so you need to convert your list of id to coma separated string for applying it in WHERE ID IN ('....') cause.

and modified query looks like this..


SQL
string query = "update Product_Details set [GatePass status]='Close' where ID in ("+ string.Join(",", (List<int>)Session["ID"]) +")";</int>
 
Share this answer
 
Comments
Member 10578683 6-Jan-15 6:31am    
Thank u. ii is working now.
Tejas Vaishnav 6-Jan-15 6:33am    
can you please rate my solution.
Debug your code and check the line
C#
list.Add(Convert.ToInt32(dr1[0]));

Also CAST your Session["ID"] into int While passing to the Update query.

Hope it helps :)
 
Share this answer
 
Comments
Tejas Vaishnav 6-Jan-15 5:32am    
Just casting is not enough for inline query, as list is object of C# and you are using it in your SQL server, so it will not understand it's list or any thing. so you need to pass any string with comma separation in WHERE ID IN (...) cause.

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