Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
code in dataaccesslayer

C#
public DataSet FindRecord(int RollNumber)
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                //string cmd = "Select * from tblstudent where RollNumber=@RollNumber";
                SqlCommand cmd = new SqlCommand("Select * from tblstudent where RollNumber=@RollNumber", con);
                cmd.Parameters.AddWithValue("@RollNumber", RollNumber);                
                SqlDataAdapter da = new SqlDataAdapter("spfindtblstudent", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();
                return ds;

            }


My stored procedure is

SQL
alter procedure spfindtblstudent
@RollNumber int
as
Begin
 Select * from tblstudent Where RollNumber=@RollNumber
 END
Posted
Updated 1-Nov-14 4:27am
v2
Comments
Mehdi Gholam 1-Nov-14 10:31am    
... and what is the exception?

1 solution

Looks like you are mixing between sending direct SQL commands and calling a stored procedure. Try to decide which one you want to use.
C#
public DataSet FindRecord(int RollNumber)
{
    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmd = "Select * from tblstudent where RollNumber=@RollNumber";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spfindtblstudent";
        cmd.Parameters.AddWithValue("@RollNumber", RollNumber);
    
        con.Open();
    
        SqlDataAdapter da = new SqlDataAdapter()
        da.SelectCommand = cmd;
    
        DataSet ds = new DataSet();
        da.Fill(ds);
    
        return ds;
    }
}


As you don't specify your exact exception message, I can only guess that this is the problem.
 
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