Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Check My code. It stores Null Value in session.
C#
for (int i = 0; i < GridView2.Rows.Count; i++)
        {
          
            string strID  = GridView2.Rows[i].Cells[1].Text;

    


            con.Open();
            using (SqlCommand command = new SqlCommand("select Role_Name from Role2 where Role_ID=@strID", con))
            { 
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@StrID"; param.Value = strID;   
                command.Parameters.Add(param); 
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Session["Role_Name"] = Convert.ToString(reader["Role_Name"]);
                }

            }
            con.Close();
        }
Posted
Updated 31-Mar-14 20:34pm
v2
Comments
Tom Marvolo Riddle 1-Apr-14 2:20am    
what's the error?
Member 10578683 1-Apr-14 2:49am    
I t is showing param value Null
Tom Marvolo Riddle 1-Apr-14 2:33am    
Try this:
while (reader.Read())
{
Var rolename = reader["Role_Name"].ToString();//Put a breakpoint here and check whether it has value or not
}
Member 10476757 1-Apr-14 2:39am    
check whether the statement by keeping break point
string strID = GridView2.Rows[i].Cells[1].Text;
and also only the last value will stored in that loop
Member 10578683 1-Apr-14 2:53am    
It store only the 1st value

1 solution

The basic difference between them is “Convert.ToString(variable)” handles NULL values even if variable value become null but “variable.ToString()” will not handle NULL values it will throw a NULL reference exception error. So as a good coding practice using “convert” is always safe. see more...[^]

So it's better that you should use .ToString() to check NULL values.

-KR
 
Share this answer
 
Comments
Code Help 2014 1-Apr-14 2:52am    
The use of Convert.ToString() will return a blank value, not NULL. So this is not the cause of the issue or the solution to the current problem.
Member 10476757 1-Apr-14 3:32am    
INITIALLY CHECK THE GRIDVIEW DOES IT HAS VALUES OR NOT BCOZ U R PASSING PARAMETER BASED ON GRIDVIEW VALUE MAKE SURE THAT THE GRIDVIEW IS LOADED WITH VALUES BEFORE EXECUTING THIS CODE

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