473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Retrieving data using SQL stored procedure in C# application

Hi,

I'm having problem retrieving data from an SQL stored procedure. I tried debugging but it wont give a the reason for the error. it just throws an exception after executing cmd.ExecuteNonQuery without any details. Can anyone please help me.. Im stuck on it since 2 days

Thanks



Stored Procedure

CREATE PROCEDURE dbo.sp_GetInformation
(
@Username VARCHAR(50) = NULL
)

AS
SELECT Address, Phone, Emergency, Email
FROM Pinfo
WHERE Username = @Username





Background C# Code
SqlConnection conn = new SqlConnection(@"Data Source=R\SQLEXPRESS;Initial Catalog=Stud;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sp_GetInformation", conn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter usr_param = cmd.Parameters.Add("@Username", SqlDbType.VarChar);
SqlParameter addr_param = cmd.Parameters.Add("@Address", SqlDbType.VarChar);
SqlParameter phone_param = cmd.Parameters.Add("@Phone", SqlDbType.Decimal);
SqlParameter mail_param = cmd.Parameters.Add("@Email", SqlDbType.VarChar);
SqlParameter emer_param = cmd.Parameters.Add("@Emergency", SqlDbType.Decimal);

usr_param.Direction = ParameterDirection.Input;
addr_param.Direction = ParameterDirection.Output;
phone_param.Direction = ParameterDirection.Output;
mail_param.Direction = ParameterDirection.Output;
emer_param.Direction = ParameterDirection.Output;

usr_param.Value = Piname.Text;

if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
cmd.ExecuteNonQuery();
}
Piaddr.Text = addr_param.Value.ToString();
Piemer.Text = emer_param.Value.ToString();
Pimail.Text = mail_param.Value.ToString();
Piphone.Text = phone_param.Value.ToString();
conn.Close();
Jan 27 '08 #1
4 18786
nateraaaa
663 Expert 512MB
If you add a try catch finally block around your code you will have an easier time finding out what the problem is.

try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
string message = ex.Message;
}
finally
{
conn.Close();
}

Nathan
Jan 28 '08 #2
Plater
7,872 Expert 4TB
Your stored procedure does not contain all of those parameters.
It only contains one input parameter.
You will need to correct your stored procedure to have those output parameters if you want to retreive all those values.
Jan 28 '08 #3
nateraaaa
663 Expert 512MB
Your stored procedure does not contain all of those parameters.
It only contains one input parameter.
You will need to correct your stored procedure to have those output parameters if you want to retreive all those values.
I agree with Plater. However if you are only concerned with the parameter used in the stored procedure you will need to modify your code to only pass a single parameter via the SqlCommand object.

nathan
Jan 28 '08 #4
Plater
7,872 Expert 4TB
It should also be noted, that since you are in fact executing a query, using ExecuteNonQuery() is probably not the correct call. You are mixing two styles together.

If you want to go with a stored procedure with only one parameter, like you are now:
You could use a DataTable and an SqlDataAdapter in your backend code to retreive the data.

If you want to go with a stored procedure that just returns parameters, like your backend code indicates:
You need to correct your SQL procedure to have the correct output parameters and change the query statement to set the parameters inside the SELECT section.
Jan 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Godzilla | last post by:
Dear all, I have a challenge in hand and am not too sure how to accomplish this using stored procedure. I have a table containing about 3 fields, but I need to reorder/renumber a field value...
1
hariharanmca
by: hariharanmca | last post by:
How to Shift or Copy Data from Sqlserver to MS Access using Stored procedure I am passing the clint MS Access file path in that i want to insert or update data in that MS Access file using...
0
by: Srireddy | last post by:
Hi all, I am trying to read an xml using stored procedure and upload it in to the DB. I am using DB2 v8.0. can anyone help me complete my assignment... Thanks in advance..
2
by: acw | last post by:
On a SQL Server 2000 db I would like to setup a stored procedure that accesses couple tables and runs the extended stored procedure xp..cmdshell. The goal is to grant users with limited privileges...
2
by: Prasad | last post by:
Hi, I am creating a stored procedure which contains an output parameter. Though the procedure is getting compiled properly I am not able to execute it from within SQL Server Management Studio and...
1
by: madhuparimi | last post by:
I am working on jasper reports.First i want to know how to generate a ireport using stored procedure. Can u please help me out and tell me how to generate an ireport using stored procedure
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
2
by: hemantc87 | last post by:
i have created this function with a parameter offset and i want to access the database using stored procedure but the code and stored procedure i have written below is not working...what is the right...
4
by: bommanaboyina | last post by:
iam using textbox1,textbox2,textbox3 for Empid,Ename,Esalary and iam upadting the data using update button for that im using stored procedure for updating ename esalary and the problem is some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.