Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-04-22
4
Medium Priority
?
906 Views
Last Modified: 2013-11-08
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

0
Comment
Question by:mrneilbarton
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 24209645
I think what your looking for is a cmd.ExecuteScalar() method..

something like

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

Author Comment

by:mrneilbarton
ID: 24209861
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
 
LVL 6

Accepted Solution

by:
RPCIT earned 2000 total points
ID: 24209955
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
 
LVL 5

Author Comment

by:mrneilbarton
ID: 24210415
perfect that is what i was looking for, now i know where i am
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

581 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