Solved

In C# Connect to database and execute Stored procedure which has output parameter

Posted on 2007-03-19
7
3,034 Views
Last Modified: 2008-01-09
I am using C# 2005 and SQL server 2005.
I already have default.aspx and web.config file in my application. i just want to connec to database and execute my stored procedure.
Can some one tell me how to do Database connection using Web.config file? as i have defined ConnectionString in Web.config file.
I have created a stored procedure in sql 2005, it takes in 2 input paramenters and return one output parameter. only one value with be return everytime as an output parameter.

Thanks for you help in advance.
0
Comment
Question by:Junior_Developer
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 18751516
have a look at this:
http://www.c-sharpcorner.com/UploadFile/dclark/InsOutsinCS11302005072332AM/InsOutsinCS.aspx

towards the bottom (right above the final "summary"), it shows you how to define a parameter for output and then get that value back....
0
 

Author Comment

by:Junior_Developer
ID: 18751804
The link above is not helpful as it is not using Web.config file's connectstring attribute.
Any other suggestions, it should be pretty straight forward. i am new at 2005 C# .net programming

Waiting for response, thanks,
0
 
LVL 15

Accepted Solution

by:
Thogek earned 500 total points
ID: 18752229
To start, make sure you know how to retrieve the connection string itself, which you can get at http://msdn2.microsoft.com/en-us/library/ms178411.aspx.

From there, you can do much as is suggested in the link samtran0331 gave, just replace

    SqlConnection cnPubs = new SqlConnection("server=localhost;integrated security=true;" + "database=pubs");

with

    SqlConnection cnPubs = new SqlConnection(connString);

where connString is populated as given in http://msdn2.microsoft.com/en-us/library/ms178411.aspx.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:sun4sunday
ID: 18754546
Go to Web.Config file

<appSettings>
  <add key="ConnectionString" value="server=servername/ IP ;database=DBName;uid=sa;password=password;"/>
</appSettings>


On FormLoad(where ever you need) you will get the sqlconnection object

SqlConnection sqlcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 18754554
For using the stored procedure check out the web , there are plently of tutorials one is below
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx

sun4sudnay
0
 

Author Comment

by:Junior_Developer
ID: 18756160
Thogek,
i am able to get the connection string now but when i use "SqlConnection dbConnection = new SqlConnection(connString);"
i get this error "The best overloaded method match for 'System.Data.SqlClient.SqlConnection.SqlConnection(string)' has some invalid arguments"

0
 
LVL 15

Expert Comment

by:Thogek
ID: 18759658
Oops.  That connString is actually a ConnectionStringSettings object.  You may need to do something more like:

    SqlConnection cnPubs = new SqlConnection(connString.ConnectionString);
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

17 Experts available now in Live!

Get 1:1 Help Now