Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public DataSet FindRecord(int RollNumber)
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                    SqlCommand cmd = new SqlCommand("spfindtblstudent", con);
                    cmd.Parameters.Add("@RollNumber", RollNumber);//
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter("cmd", con);
                    con.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    con.Close();
                    return ds;
                    
                
            }
        }


the error iam getting is Here
C#
cmd.Parameters.Add("@RollNumber", RollNumber);//

while adding values to parameters getting the error mentioned how to get rid of that i have used the Addwithparameters but not getting it correct

Help me
Posted
Updated 31-Oct-14 22:21pm
v3

replace cmd.Parameters.Add("@RollNumber", RollNumber);
to
C#
cmd.Parameters.AddWithValue("@RollNumber", RollNumber);


from MSDN
Quote:
Use caution when you are using this overload of the SqlParameterCollection.Add method to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.
parameters.Add("@pname", Convert.ToInt32(0));
If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameterCollection.Add (string, SqlDbType) overload.
 
Share this answer
 
v2
Comments
raxhemanth 1-Nov-14 4:27am    
Thankyou so much DamithSL its gone
It's not an error - it's a warning. But what it is saying is that the Parameters.Add method is depreciated - there is a more modern, better version - and that you should use that instead. As of the latest version of the documentation, the Add method is obsolete, which means that it could be removed from future versions of the framework. You shouldn't use it in new code as a result.

SqlParameter.Add is normally only used when you need a lot more control over what is going on, but it's a lot longer code. In this case, It's just a "completeness" thing:
C#
cmd.Parameters.AddWithValue("@RollNumber", RollNumber);
Will fix it.
 
Share this answer
 

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