943,840 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3071
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 3rd, 2009
0

Execute Stored Procedure From Form

Expand Post »
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
Similar Threads
MJV
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MJV is offline Offline
66 posts
since Feb 2009
Mar 3rd, 2009
0

Re: Execute Stored Procedure From Form

Please search this forum, this has been answered a number of times before.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 3rd, 2009
0

Re: Execute Stored Procedure From Form

There and you can check here
http://support.microsoft.com/kb/320916
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Mar 3rd, 2009
0

Re: Execute Stored Procedure From Form

Click to Expand / Collapse  Quote originally posted by LizR ...
Please search this forum, this has been answered a number of times before.
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.
MJV
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MJV is offline Offline
66 posts
since Feb 2009
Mar 3rd, 2009
0

Re: Execute Stored Procedure From Form

Click to Expand / Collapse  Quote originally posted by LizR ...
Please search this forum, this has been answered a number of times before.
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);
}
MJV
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MJV is offline Offline
66 posts
since Feb 2009
Mar 3rd, 2009
0

Re: Execute Stored Procedure From Form

I guess my replies are either completely wrong or not worth looking at - seems to me on that link I posted was this

C# Syntax (Toggle Plain Text)
  1. //Create a connection to the SQL Server; modify the connection string for your environment
  2. //SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;Trusted_Connection=yes");
  3. SqlConnection MyConnection = new SqlConnection("server=(local);database=pubs;UID=myUser;PWD=myPassword;");
  4.  
  5. // Create a Command object, and then set the connection.
  6. // The following SQL statements check whether a GetAuthorsByLastName
  7. // stored procedure already exists.
  8. SqlCommand MyCommand = new SqlCommand("select * from sysobjects where id = object_id(N'GetAuthorsByLastName')" +
  9. " and OBJECTPROPERTY(id, N'IsProcedure') = 1", MyConnection);
  10.  
  11. // Set the command type that you will run.
  12. MyCommand.CommandType = CommandType.Text;
  13.  
  14. // Open the connection.
  15. MyCommand.Connection.Open();
  16.  
  17. // Run the SQL statement, and then get the returned rows to the DataReader.
  18. 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?
Reputation Points: 155
Solved Threads: 41
Posting Whiz in Training
rapture is offline Offline
294 posts
since Jul 2007
Mar 4th, 2009
0

Re: Execute Stored Procedure From Form

Click to Expand / Collapse  Quote originally posted by MJV ...
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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Mar 4th, 2009
0

Re: Execute Stored Procedure From Form

If the form is attached to the database through table adapters do I have to make another SqlConnection assignment?
MJV
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MJV is offline Offline
66 posts
since Feb 2009
Mar 4th, 2009
0

Re: Execute Stored Procedure From Form

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();
}
MJV
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
MJV is offline Offline
66 posts
since Feb 2009
Mar 4th, 2009
1

Re: Execute Stored Procedure From Form

Okay, I see you are getting nowhere, so I will give you the code to do this:

C# Syntax (Toggle Plain Text)
  1.  
  2. private void DoSomething()
  3. {
  4. string ConnectionString = "Data Source={0};Initial Catalog={1};Integrated Security=True";
  5.  
  6. int estno;
  7. if( int.TryParse(startestnoCB.Text,out estno) )
  8. {
  9. SqlConnection conn = new SqlConnection(
  10. string.Format(ConnectionString,".//SqlExpress" // Your Sql Server
  11. , "EstimateDataSet" // Your SQL Database
  12. ));
  13.  
  14. SqlCommand cmd = new SqlCommand("EstimateApproval", conn);
  15. cmd.CommandType = CommandType.StoredProcedure;
  16. cmd.Parameters.AddWithValue("@Estno", estno);
  17. try
  18. {
  19. conn.Open();
  20. #if WantSomethingBack
  21. DataTable result = new DataTable();
  22. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  23. adapter.Fill(result);
  24. foreach(DataRow row in result.Rows)
  25. {
  26.  
  27. }
  28. result.Dispose()
  29. #else
  30. cmd.ExecuteNonQuery(); // If you do not need a return;
  31. #endif
  32. }
  33. catch (SqlException err)
  34. {
  35. MessageBox.Show(err.Message);
  36. }
  37. finally
  38. {
  39. if(conn.State == ConnectionState.Open)
  40. conn.Close();
  41. conn.Dispose();
  42. }
  43. }
  44. }
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC