Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to call a procedure but it returns nothing and when I debug it displays null but my table contain a record

below is my code :

C#
protected void Button1_ServerClick(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("LoginProcedure", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("@student_id", Text1.Value);
            cmd.Parameters.Add(p1);
            con.Open();
            cmd.ExecuteNonQuery();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Label2.Text = dr.GetString(0);
            }
        }
    }
}


this is my procedure :

SQL
CREATE PROCEDURE [dbo].[LoginProcedure]
	@student_id as varchar(10)
AS
	SELECT student_id,dob from Personal where student_id=@student_id
RETURN 0


What I have tried:

I tried many different things and I used textbox and button as HTML controls they are HTML controls with runat=server property true and I also try to change my procedure with query but still it returns nothing
Posted
Updated 26-Mar-16 22:41pm
Comments
Member 11712753 27-Mar-16 5:51am    
It looks like cmd.ExecuteNonQuery(); is useless in the code block. Try to remove the line and see if it works?

1 solution

Try this:
C#
SqlParameter p1 = new SqlParameter("@student_id", SqlDbType.VarChar, Text1.value, "student_id");
p1.IsNullable = true;
// p1.Direction = ParameterDirection.Input;  // Not needed, default.

Maybe you also need Text1.Text instead of Text1.value.

Here is a good article on CodeProject:
Using SqlParameter Direction in C#.Net with with Stored Procedure[^]
 
Share this answer
 
v2
Comments
Member 11712753 27-Mar-16 5:42am    
I think it is not appropriate to declare the SqlParameter to be an output parameter in this case.
RickZeeland 27-Mar-16 5:48am    
You are right ! I will update the solution, thanks.

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