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

Posted on 2009-04-22
Last Modified: 2013-11-08

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

Question by:mrneilbarton
    LVL 6

    Expert Comment

    I think what your looking for is a cmd.ExecuteScalar() method..

    something like

    Int64 myCoutner = Convert.ToInt64( cmd.ExecuteScalar().ToString());
    LVL 5

    Author Comment

    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#
    LVL 6

    Accepted Solution

    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.
    LVL 5

    Author Comment

    perfect that is what i was looking for, now i know where i am

    Featured Post

    Live - One-on-One C# Help from Top Experts

    Solve your toughest problems, fast.
    C# experts are online now and ready to help you.

    Join & Write a Comment

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now