Solved

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

Posted on 2007-03-19
7
3,029 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:sun4sunday
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

12 Experts available now in Live!

Get 1:1 Help Now