ASP.NET C# Get Value From Stored Procedure Example - Slightly Urgent

Hi,  

Need a quick example of a stored procedure in C#, normally programme in VB so getting slightly annoyed with this at the moment as nothing i do seems to work.

The code i have so far is below, i just need the SQL reader bit in c# and grab the column memberCounter and store it in a variable, an full example would be great.
sqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
 
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
            conn.Open();
 
            string SQLStr = "bart_CheckEmail";
            cmd.CommandText = SQLStr;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 60;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = fldEmailSignUp.Text;
            cmd.Connection = conn;
                               
           *THIS IS WHERE I WANT TO READ THE COLUMN memberCounter and store it in a varable*
  
            conn.Close();

Open in new window

LVL 5
mrneilbartonAsked:
Who is Participating?
 
RPCITCommented:
Are you wanting a single value from the stored procedure?  or are you wanting a recordset?  

If you want a "recordset".. you need to create a SQLDataReader...

SqlDataReader dr =  cmd.ExecuteReader();
while(dr.Read())
{
     string val = dr["memberCount"].ToString();
}

but if your stored procedure only returns one row, executescaler is probably more like what you want.
0
 
RPCITCommented:
I think what your looking for is a cmd.ExecuteScalar() method..

something like

Int64 myCoutner = Convert.ToInt64( cmd.ExecuteScalar().ToString());
0
 
mrneilbartonAuthor Commented:
i dont understand how that can retreive the column memberCount with vb you can do rs("memberCount") - nice and simple - what is the same for c#
0
 
mrneilbartonAuthor Commented:
perfect that is what i was looking for, now i know where i am
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.