Solved

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

Posted on 2007-03-19
7
3,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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