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


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;
            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*

Open in new window

Who is Participating?
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();
     string val = dr["memberCount"].ToString();

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

something like

Int64 myCoutner = Convert.ToInt64( cmd.ExecuteScalar().ToString());
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#
mrneilbartonAuthor Commented:
perfect that is what i was looking for, now i know where i am
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.