473,396 Members | 2,115 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,396 software developers and data experts.

How to execute a stored procedure in C#

62
Hi all

I have a program which needs to update a sql server database table.

I have a stored procedure called: "spPastelConfig"

My connection is passed in a property canned "scnn".

Can someone tell me what is wrong with my code? I keep getting this error:

"System.Data ExecuteNonQuery: Connection property has not been initialized"

I know I have to pass the connection some where, but not sure where. Can someone please provide me with the correct code?

Thanks in advance,

Lóan
Jan 29 '08 #1
8 2644
Shashi Sadasivan
1,435 Expert 1GB
You havent providedd your code for us to comment on.

However have you set the command type to Stored procedure?
Jan 29 '08 #2
LoanB
62
Oh yeah - sorry:

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spPastelConfig";
cmd.Parameters.AddWithValue("@ServerName", this.ServerName);
cmd.Parameters.AddWithValue("@dbName", this.DatabaseName);
scnn.OpenConn();

cmd.ExecuteNonQuery();
Jan 29 '08 #3
Shashi Sadasivan
1,435 Expert 1GB
you have to pass the connection string to scnn (which tells it where it is connecting to)
At the moment scnn has no clue where to contact to request for the stored procedure
Jan 29 '08 #4
ilearneditonline
130 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. using (SqlConnection cn = new SqlConnection(<your connection string>)
  3. SqlCommand cmd = new SqlCommand("spPastelConfig", cn);
  4. cmd.Parameters.Add("@ServerName", SqlDbType.VarChar, 50).Value = this.ServerName;
  5. cmd.Parameters.Add("@dbName", SqlDbType.VarChar, 50).Value = this.DatabaseName;
  6. cmd.CommandType = CommandType.StoredProcedure;
  7. cn.Open();
  8. cmd.ExecuteNonQuery();
  9. }
  10.  
This is how I do this.
Jan 29 '08 #5
LoanB
62
Excellent.

Thanks for that!
Feb 8 '08 #6
krille
26
Have to say that i prefer doing it with an sql statement, like this:

Expand|Select|Wrap|Line Numbers
  1. string strSql  = "spPastelConfig @ServerName='"+this.ServerName+"' @dbName='"+this.DatabaseName+"'";
  2. SqlCommand cmd = new SqlCommand(strSql, cn);
  3. cn.Open();
  4. cmd.ExecuteNonQuery();
  5.  
just my personal oppinion...
Feb 8 '08 #7
Shashi Sadasivan
1,435 Expert 1GB
the conneting is string is good to be stored at a central place
the best places is app.config , or web.config depending upon your application type.
Feb 9 '08 #8
LoanB
62
Thanks all

Advice is always greatly appreciated. I consider it all.
Feb 15 '08 #9

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

Similar topics

1
by: PJ | last post by:
<% Set Conn = Server.CreateObject("ADODB.Connection") Conn.open application("dtat_motor_connectionstring") set rs = new adodb.recordset 'Set RS = Conn.Execute(' "exec spcn_update_transactions &...
3
by: Richard Morey | last post by:
Hi.. I have written a stored procedure that take 5 - 8 minutes to fully execute.. I wrote this routine as a stored procedure because I started to create all the functionality via ASP but I kept...
2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
3
by: Zeke Hoskin | last post by:
Suddenly a stored procedure, very much like several others, is giving EXECUTE permission denied on object 'Add_Adjustment', database 'InStab', owner 'zhoskin'. server:Msg 229, Level 14, State 5,...
1
by: HD | last post by:
Is there a way to let an account have execute permission on a stored procedure but not let that stored procedure run insert , delete, or update records. Basically only let them run or create stored...
1
by: robin via SQLMonster.com | last post by:
I've tried several different way to execute a oracle stored procedure from a DTS package but to no avail. I have a Linked Server setup which does bring back Oracle tables from the server when I...
2
by: Noloader | last post by:
Hello, Access XP, SQL Server 2000 Is it possible to hide a SP under Queries in Access, yet still be able to Execute it from Access? (Similar to hiding Tables, then using Views) We hooked...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
7
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability,...
0
by: franjorge | last post by:
Hi, I have created two stored procedures via VB using this code: sql = "CREATE PROC " & nombre_proc & " AS SELECT *" & _ " From MBM_PUNTOS_SCE_SIN_COINCIDIR_SIEGE_FALTA_PM_NE_" & mes & _ "...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.