473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving data using SQL stored procedure in C# application

1 New Member
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.ExecuteNonQ uery without any details. Can anyone please help me.. Im stuck on it since 2 days

Thanks



Stored Procedure

CREATE PROCEDURE dbo.sp_GetInfor mation
(
@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\SQLEXP RESS;Initial Catalog=Stud;In tegrated Security=True") ;
SqlCommand cmd = new SqlCommand("sp_ GetInformation" , conn);
cmd.CommandType = CommandType.Sto redProcedure;

SqlParameter usr_param = cmd.Parameters. Add("@Username" , SqlDbType.VarCh ar);
SqlParameter addr_param = cmd.Parameters. Add("@Address", SqlDbType.VarCh ar);
SqlParameter phone_param = cmd.Parameters. Add("@Phone", SqlDbType.Decim al);
SqlParameter mail_param = cmd.Parameters. Add("@Email", SqlDbType.VarCh ar);
SqlParameter emer_param = cmd.Parameters. Add("@Emergency ", SqlDbType.Decim al);

usr_param.Direc tion = ParameterDirect ion.Input;
addr_param.Dire ction = ParameterDirect ion.Output;
phone_param.Dir ection = ParameterDirect ion.Output;
mail_param.Dire ction = ParameterDirect ion.Output;
emer_param.Dire ction = ParameterDirect ion.Output;

usr_param.Value = Piname.Text;

if (conn.State.Equ als(ConnectionS tate.Closed))
{
conn.Open();
cmd.ExecuteNonQ uery();
}
Piaddr.Text = addr_param.Valu e.ToString();
Piemer.Text = emer_param.Valu e.ToString();
Pimail.Text = mail_param.Valu e.ToString();
Piphone.Text = phone_param.Val ue.ToString();
conn.Close();
Jan 27 '08 #1
4 18818
nateraaaa
663 Recognized Expert Contributor
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.ExecuteNonQ uery();
}
catch(Exception ex)
{
string message = ex.Message;
}
finally
{
conn.Close();
}

Nathan
Jan 28 '08 #2
Plater
7,872 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Expert
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
10454
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 every time there is an insert or delete on the table. Below is the table structure: tableID customerID pageID ===== ======== ====== 0 1 0
1
2305
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 Store Procedure. Thank you
0
2016
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
4090
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 the right to run the stored procedure but not the rights to directly access either the referenced tables or the extended stored procedure. TIA!
2
1027
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 also through an aspx page. I keep getting empty output (not null) The code: Create procedure LoginUser
1
5403
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
14659
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 can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
2
3022
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 way to do it?? public bool isoffsetexist(int offset) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure;
4
1995
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 times i dont want to update the Ename but i wrote the stored procedure for updating both ename and esalary as well....when iam leaving the textbox2 as empty iam getting the errors so how can i modify my stored procedure with if statement for accepting...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.