| | |
How to connect a SQL stored procedure to a windows application?
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2007
Posts: 15
Reputation:
Solved Threads: 0
Hi,
I have created a stored procedure that would insert new records in one of my database tables. I have tested the procedure in SQL Server 2005 and it works all right. What I want to do next is to connect the stored procedure to my form in VB.Net so that I can input new records using the form. In the form I have 3 text boxes (for the 3 atributes in the table) and a button that would call the procedure (therefore assigning the values from the text boxes as a new record with 3 atributes in my table). I have already created a BindingSource and DataSet that refer to the stored procedure. How can I call the procedure and make it take the values of the 3 textboxes by clicking the button?
Thanks in advance,
Robert
I have created a stored procedure that would insert new records in one of my database tables. I have tested the procedure in SQL Server 2005 and it works all right. What I want to do next is to connect the stored procedure to my form in VB.Net so that I can input new records using the form. In the form I have 3 text boxes (for the 3 atributes in the table) and a button that would call the procedure (therefore assigning the values from the text boxes as a new record with 3 atributes in my table). I have already created a BindingSource and DataSet that refer to the stored procedure. How can I call the procedure and make it take the values of the 3 textboxes by clicking the button?
Thanks in advance,
Robert
•
•
Join Date: Jan 2007
Posts: 15
Reputation:
Solved Threads: 0
Hi,
I did some reading and I came up with this code that would connect to the SQL stored procedure (SpecialtyInsert) from my VB.Net form, however, it doesn't seem to work. It doesn't give me any errors, it just does not assign the parameters as values for the new record. Could somebody help me locate the problem? Thanks.
Here is the code:
I did some reading and I came up with this code that would connect to the SQL stored procedure (SpecialtyInsert) from my VB.Net form, however, it doesn't seem to work. It doesn't give me any errors, it just does not assign the parameters as values for the new record. Could somebody help me locate the problem? Thanks.
Here is the code:
VB.NET Syntax (Toggle Plain Text)
Dim myConnectionString As String = "Data Source=rna040256\sqlexpress;Integrated Security=True" Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(myConnectionString) myConnection.Open() Try Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("SpecialtyInsert", myConnection) command.CommandType = CommandType.StoredProcedure command.Parameters.AddWithValue("@Skill_ID", TextBox1.Text) command.Parameters.AddWithValue("@Skill_Descriptoin", TextBox2.Text) command.Parameters.AddWithValue("@Pay_Rate_Per_Hour", TextBox3.Text) Finally myConnection.Close() End Try
howdy there!
The code listing you rendered did not take cognisance of the data type being sent to the sprocs.
See the example below direct from my code and it works and I hope it helps you.
This function takes one string parameter and returns a datatable
The datatype being set in your form must be equal to the datatype of the parameter of the sproc. It is fundamental or else it won't work.
Try it and get back to me.
The code listing you rendered did not take cognisance of the data type being sent to the sprocs.
See the example below direct from my code and it works and I hope it helps you.
This function takes one string parameter and returns a datatable
VB.NET Syntax (Toggle Plain Text)
Public Function ScheduledFlights(ByVal strFlightDate As String) As DataTable Dim paramSql As New SqlClient.SqlParameter dtSchedule = New DataTable 'sets the sproc commFlights = New SqlClient.SqlCommand("procFlightSchedule") commFlights.CommandType = CommandType.StoredProcedure commFlights.Connection = connFlight paramSql.ParameterName = "@flightDate" 'pay attention to this detail paramSql.SqlDbType = SqlDbType.Char paramSql.Size = 10 paramSql.Value = strFlightDate commFlights.Parameters.Add(paramSql) 'runs the sproc adpt_Flight2.Fill(dtSchedule)
The datatype being set in your form must be equal to the datatype of the parameter of the sproc. It is fundamental or else it won't work.
Try it and get back to me.
•
•
•
•
Hi,
I did some reading and I came up with this code that would connect to the SQL stored procedure (SpecialtyInsert) from my VB.Net form, however, it doesn't seem to work. It doesn't give me any errors, it just does not assign the parameters as values for the new record. Could somebody help me locate the problem? Thanks.
Here is the code:
VB.NET Syntax (Toggle Plain Text)
Dim myConnectionString As String = "Data Source=rna040256\sqlexpress;Integrated Security=True" Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(myConnectionString) myConnection.Open() Try Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("SpecialtyInsert", myConnection) command.CommandType = CommandType.StoredProcedure command.Parameters.AddWithValue("@Skill_ID", TextBox1.Text) command.Parameters.AddWithValue("@Skill_Descriptoin", TextBox2.Text) command.Parameters.AddWithValue("@Pay_Rate_Per_Hour", TextBox3.Text) Finally myConnection.Close() End Try
Havung set up your command object and added the parameters, you have neglected to call
Do take note on Jamello about datatypes. The data in your textboxes are all strings. We don't know what your Sql Server table columns datatype is (you don't tell us) but if they're not char, varchar or nvarchar you are going to get an error.
command.ExecuteNonQuery() to actually execute the procedure.Do take note on Jamello about datatypes. The data in your textboxes are all strings. We don't know what your Sql Server table columns datatype is (you don't tell us) but if they're not char, varchar or nvarchar you are going to get an error.
•
•
Join Date: Jan 2007
Posts: 15
Reputation:
Solved Threads: 0
Hi,
Thanks for the help. I added command.ExecuteNonQuery() to the code and, surprisingly, it works. I guess the conversion of the data types is made automatically (the datatypes of the atributes are char(10), varchar(50) and int). However, it doesnot update the table with the new record until I restart the form. I tried using DataGridView1.Update() but it doesn't do the job, the record doesn't show in the table until I restart the form (I am also refreshing the datagrid, nothing happens). How can I update the table without restarting the form? ex. Just loading the table into the datagrid again during runtime (I have a button for that).
Thanks for the help so far.
Thanks for the help. I added command.ExecuteNonQuery() to the code and, surprisingly, it works. I guess the conversion of the data types is made automatically (the datatypes of the atributes are char(10), varchar(50) and int). However, it doesnot update the table with the new record until I restart the form. I tried using DataGridView1.Update() but it doesn't do the job, the record doesn't show in the table until I restart the form (I am also refreshing the datagrid, nothing happens). How can I update the table without restarting the form? ex. Just loading the table into the datagrid again during runtime (I have a button for that).
Thanks for the help so far.
•
•
Join Date: Jan 2007
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
Hi,
Thanks for the help. I added command.ExecuteNonQuery() to the code and, surprisingly, it works. I guess the conversion of the data types is made automatically (the datatypes of the atributes are char(10), varchar(50) and int). However, it doesnot update the table with the new record until I restart the form. I tried using DataGridView1.Update() but it doesn't do the job, the record doesn't show in the table until I restart the form (I am also refreshing the datagrid, nothing happens). How can I update the table without restarting the form? ex. Just loading the table into the datagrid again during runtime (I have a button for that).
Thanks for the help so far.
![]() |
Similar Threads
- How to display a table on windows application in c# (C#)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Stored procedure call with ADO (C)
Other Threads in the VB.NET Forum
- Previous Thread: Searching through a Directory including all subs
- Next Thread: Q&A VB.NET and Truncated Email - 2 Examples
| Thread Tools | Search this Thread |
.net .net2008 2008 access account add advanced application array basic beginner browser button buttons click code combo cpu cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic employees excel exists fade filter forms generatetags html images input intel internet listview mobile module monitor mysql net number objects open panel pdf picturebox picturebox2 port position print printing printpreview problem regex reuse right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver storedprocedure survey temperature textbox timer timespan transparency txttoxmlconverter update user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vba vbnet vista visual visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode xml year






