Execute Stored Procedure From Form

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 66
Reputation: MJV is an unknown quantity at this point 
Solved Threads: 0
MJV MJV is offline Offline
Junior Poster in Training

Execute Stored Procedure From Form

 
0
  #1
Mar 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Execute Stored Procedure From Form

 
0
  #2
Mar 3rd, 2009
Please search this forum, this has been answered a number of times before.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Execute Stored Procedure From Form

 
0
  #3
Mar 3rd, 2009
There and you can check here
http://support.microsoft.com/kb/320916
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 66
Reputation: MJV is an unknown quantity at this point 
Solved Threads: 0
MJV MJV is offline Offline
Junior Poster in Training

Re: Execute Stored Procedure From Form

 
0
  #4
Mar 3rd, 2009
Originally Posted by LizR View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 66
Reputation: MJV is an unknown quantity at this point 
Solved Threads: 0
MJV MJV is offline Offline
Junior Poster in Training

Re: Execute Stored Procedure From Form

 
0
  #5
Mar 3rd, 2009
Originally Posted by LizR View Post
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);
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Execute Stored Procedure From Form

 
0
  #6
Mar 3rd, 2009
I guess my replies are either completely wrong or not worth looking at - seems to me on that link I posted was this

  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Execute Stored Procedure From Form

 
0
  #7
Mar 4th, 2009
Originally Posted by MJV View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 66
Reputation: MJV is an unknown quantity at this point 
Solved Threads: 0
MJV MJV is offline Offline
Junior Poster in Training

Re: Execute Stored Procedure From Form

 
0
  #8
Mar 4th, 2009
If the form is attached to the database through table adapters do I have to make another SqlConnection assignment?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 66
Reputation: MJV is an unknown quantity at this point 
Solved Threads: 0
MJV MJV is offline Offline
Junior Poster in Training

Re: Execute Stored Procedure From Form

 
0
  #9
Mar 4th, 2009
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();
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Execute Stored Procedure From Form

 
1
  #10
Mar 4th, 2009
Okay, I see you are getting nowhere, so I will give you the code to do this:

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC