| | |
Execute Stored Procedure From Form
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 66
Reputation:
Solved Threads: 0
I have created and tested a stored procedure that works in sql. This stored procedure has one parameter that it requires to run "quoteno" . I already have the database attached to the form. Can anyone help me with the code for this. The name of my stored procedure is EstimateApproval and my database name is Estimator.
Thanks. Ive looked for answers on the net but not having any luck
Thanks. Ive looked for answers on the net but not having any luck
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
There and you can check here
http://support.microsoft.com/kb/320916
http://support.microsoft.com/kb/320916
•
•
Join Date: Feb 2009
Posts: 66
Reputation:
Solved Threads: 0
•
•
•
•
Please search this forum, this has been answered a number of times before.
private void approveestBN_Click(object sender, EventArgs e)
{
int @estno;
int.TryParse(startestnoCB.Text,out estno);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "EstimateApproval";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@estno", SqlDbType.Int);
}
•
•
Join Date: Jul 2007
Posts: 276
Reputation:
Solved Threads: 37
I guess my replies are either completely wrong or not worth looking at - seems to me on that link I posted was this
maybe you have some of this stuff other places, like the connection etc.
If I'm way off, please ignore me as I'm still fairly new.
Did you step through it to see what happens?
C# Syntax (Toggle Plain Text)
//Create a connection to the SQL Server; modify the connection string for your environment //SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Trusted_Connection=yes"); SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;UID=myUser;PWD=myPassword;"); // Create a Command object, and then set the connection. // The following SQL statements check whether a GetAuthorsByLastName // stored procedure already exists. SqlCommand MyCommand = new SqlCommand("select * from sysobjects where id = object_id(N'GetAuthorsByLastName')" + " and OBJECTPROPERTY(id, N'IsProcedure') = 1", MyConnection); // Set the command type that you will run. MyCommand.CommandType = CommandType.Text; // Open the connection. MyCommand.Connection.Open(); // Run the SQL statement, and then get the returned rows to the DataReader. SqlDataReader MyDataReader = MyCommand.ExecuteReader();
maybe you have some of this stuff other places, like the connection etc.
If I'm way off, please ignore me as I'm still fairly new.
Did you step through it to see what happens?
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
•
•
•
•
This is what i have tried. I get no errors but the database is not changing as per the stored procedure which does work when run within sql.
private void approveestBN_Click(object sender, EventArgs e)
{
int @estno;
int.TryParse(startestnoCB.Text,out estno);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "EstimateApproval";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@estno", SqlDbType.Int);
}
Where is your SqlConnection assignment to the SqlCommand ?
Where is your value assignment to the cmdParameter ?
Where is your Execute command on cmd ?
Last edited by JerryShaw; Mar 4th, 2009 at 10:26 am.
•
•
Join Date: Feb 2009
Posts: 66
Reputation:
Solved Threads: 0
I have tried this but Im now getting an error saying that "Format of the initialization string does not conform to specification starting at index 0."
private void approveestBN_Click(object sender, EventArgs e)
{
int @estno;
int.TryParse(startestnoCB.Text,out estno);
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "estimateDataSet";
cmd.Connection = conn;
cmd.CommandText = "EstimateApproval";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@estno", SqlDbType.Int);
cmd.ExecuteReader();
}
private void approveestBN_Click(object sender, EventArgs e)
{
int @estno;
int.TryParse(startestnoCB.Text,out estno);
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "estimateDataSet";
cmd.Connection = conn;
cmd.CommandText = "EstimateApproval";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@estno", SqlDbType.Int);
cmd.ExecuteReader();
}
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Okay, I see you are getting nowhere, so I will give you the code to do this:
C# Syntax (Toggle Plain Text)
private void DoSomething() { string ConnectionString = "Data Source={0};Initial Catalog={1};Integrated Security=True"; int estno; if( int.TryParse(startestnoCB.Text,out estno) ) { SqlConnection conn = new SqlConnection( string.Format(ConnectionString,".//SqlExpress" // Your Sql Server , "EstimateDataSet" // Your SQL Database )); SqlCommand cmd = new SqlCommand("EstimateApproval", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Estno", estno); try { conn.Open(); #if WantSomethingBack DataTable result = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(result); foreach(DataRow row in result.Rows) { } result.Dispose() #else cmd.ExecuteNonQuery(); // If you do not need a return; #endif } catch (SqlException err) { MessageBox.Show(err.Message); } finally { if(conn.State == ConnectionState.Open) conn.Close(); conn.Dispose(); } } }
![]() |
Similar Threads
- SQL Server IMAGE and VB.NET (VB.NET)
- NEWBIE - Insert data from ASP.Net application into mutiple SQL tables (ASP.NET)
- Login used to work (ASP.NET)
- ADO.NET question modification. (VB.NET)
- recordset from multiple checkbox values (ASP)
Other Threads in the C# Forum
| Thread Tools | Search this Thread |
.net access algorithm animation array barchart bitmap box broadcast c# check checkbox client code combobox control conversion csharp custom database datagrid datagridview dataset datastructure datetime degrees development directrobot draganddrop drawing encryption enum event excel file form format forms function gdi+ hash httpwebrequest image index input install java label lisp list listbox mandelbrot math mouseclick mp3 mysql native operator packaging path photoshop picturebox pixelinversion post print process programming radians regex remote remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser wfa windows winforms wpf xml






