I have a small requirement and i request anyone to help me out by providing the source code for the same.
The requirement is as follows:
Can you please help me as to how to call a stored procedure by passing paramenters and populate a CheckListBox in VB.NET based on the results returned from the stored procedure.
Regards,
George
Imports System.Data
Imports System.Data.SqlClient
import that namespace and write the following code to some method and change your code According to your Database and Procedure Fields
'Set up Connection object and Connection String for a SQL Client
Using SQLCon As New SqlClient.SqlConnection
SQLCon.ConnectionString = "Data Source=Server;User ID=User;Password=Password;"
SQLCon.Open()
Dim lDataTable As New DataTable
Using SQLCmdAs New SqlCommand()
SQLCmd.CommandText = "GetAuthors" ' Stored Procedure to Call
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Connection = SQLCon
SQLCmd.Parameters.Add("AuthorName", SqlDbType.VarChar)
Dim daPubs As New SqlDataAdapter
daPubs.SelectCommand = SQLCmd
daPubs.Fill(lDataTable)
End Using
End Using
CheckBoxList1.DataSource = lDataTable;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "ID"
CheckBoxList1.DataBind();
Using Stored Procedures in Conjuction with DataAdapter
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET
Related
I am using the built-in database for my program. When I try to put in the connection string, VB cannot detect the connection string and shows a syntax error on line 7 after the new SqlConnection. I am sure that I copied the complete connection string from the properties page.
I read this post but it seems to be a different question. Below is my code for the connection. Is there any mistake in my code? Thanks for all the help!
Imports System.Data.SqlClient
Public Class Login
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim sql As String
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")
That'll obviously show you a syntax error, look at your following line:
"C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"
Replace the double-quotes to ""<abc>"" to get like "<abc>" on execution because you've already used "<abc>" in New SqlConnection("...").
Rather than:
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf";Integrated Security=True")
you should have:
Dim conn As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""C:\Users\zhenwei\source\repos\Cafeteria Ordering System v1.0\Cafeteria Ordering System v1.0\Database1.mdf"";Integrated Security=True")
I am trying to link a VB application with a MS access database.
The only way I seem able to do this is through adding a data source. Is there a way or doing this so you use it as an external and code to the file location rather than actually uploading the database to vb?
Thanks
Imports System.Data.Oledb
Dim con As New OledbConnection("Provider=microsoft.Jet.oledb.4.0DataSource=D:\mydata.mdb;")
Dim cmd As New OledbCommand
Public Sub New()
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM table1"
End Sub
Using Visual Studio 2010 I have created a VB Web Application which I added a database to. I have a VB class which is suppose to take a name and add it to the database through a stored procedure. I run the code and it passes the variable to the vb class but doesn't seem to change the table once I've stopped running the program.
I'm new to the vb and visual studios thing so I'm using http://www.macronimous.com/resources/calling_stored_procedures_from_ASP.NET_and_VB.NET.asp as my guide which is why the code is the way it is.
Here is the vb class
Imports System.Data
Imports System.Data.SqlClient
Public Class human
Private name As String
Private id As Integer
Public Function sendName(ByVal nm As String)
Dim SQLCon As New SqlClient.SqlConnection
Dim SQLCmd As New SqlCommand
SQLCon.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=
C:\Documents and Settings\user\my documents\visual studio 2010\Projects\
WebApplication3\WebApplication3\App_Data\Database1.mdf;
Integrated Security=True;User Instance=True"
SQLCon.Open()
'I want to to execute the procedure addName using the variable nm
SQLCmd.CommandText = "addName"
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Parameters.AddWithValue("#name", nm)
SQLCmd.Connection = SQLCon 'Active Connection
SQLCon.Close()
Return (nm)
End Function
End Class
and here's my stored procedure "addName"
Alter PROCEDURE addName (#GivenName varchar(100))
AS
BEGIN
SET NOCOUNT ON;
Insert into tableA (name) values (#GivenName);
END
return
What am I missing that prevents the table from showing updates after I stop debugging the application?
Great. You define a command, you create a connection and open it. But you fail to execute the command.
SQLCmd.Connection = SQLCon 'Active Connection
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
You omitted a statement SQLCmd.ExecuteNonQuery()
SQLCmd.ExecuteNonQuery() 'You need this line
SQLCon.Close()
You missed the ExecuteNonQuery() method.
SQLCon.Open()
SQLCmd.CommandText = "addName"
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Parameters.AddWithValue("#name", nm)
SQLCmd.Connection = SQLCon 'Active Connection
SQLCmd.ExecuteNonQuery() 'This one is missing
SQLCon.Close()
I'm a novice VB programmer and I'm sure this solution is trivial, however, I am getting the above error when I try to display the results of one of my Stored Procs in SQL Server which doesn't need any parameters. How can I fix this?
My code excerpt:
Dim SQLCmd as SQLCommand = new SQLCommand()
SQLCmd.CommandText = "Exec AllTableCount"
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Connection = GlobalFunctions.GlobalF.GetDevSQLServerStoredProcedure(SQLCmd.CommandType)
SQLCmd.ExecuteNonQuery()
MsgBox(SQLCmd.ExecuteNonQuery)
Where GetDevSQLServerStoredProcedure is defined in a different file as:
Public Shared Function GetDevSQLServerStoredProcedure(ByVal SQL As String)
Dim DBConn As SQLConnection
Dim DBCommand As SQLDataAdapter
Dim DSPageData As New System.Data.DataSet
DBConn = New SQLConnection(ConfigurationSettings.AppSettings("AMDMetricsDevConnectionString"))
DBCommand = New SQLDataAdapter(SQL) 'This is the line it errors on'
DBCommand.Fill(DSPageData, "Exceptions")
Return DSPageData
End Function
I can see this SP from VS 2008 in the Server Explorer. So the problem seems to be that I don't know how to connect a data adapter to an SP. Just a string query.
Command text should just be the name of the stored procedure as Tim mentioned.
It looks like the problem you are having now is that you are passinging the CommandType to your method GetDevSQLServerStoredProcedure instead of a string.
Can anyone please help me on the following:
I have created a SQL Server stored procedure as follows:
create procedure prcGet_sub_menu_list
#sub_menu char(5)
as
begin
select
'menu_code' = menu_code
'menu_name' = menu_name
from sub_menu_master
where menu_code = #sub_menu
end
return
Now i am calling this procedure from VB.NET, but i get an error like 'Stored procedure prcGet_sub_menu_list expects parameter #sub_menu which was not supplied'. Please help me on the same. The code which i have in VB.NET is as follows:
Imports System.Data
Imports System.Data.SqlClient
Dim sqlConn as New SqlClient.SqlConnection
sqlConn.ConnectionString = "........"
sqlConn.Open()
Dim menuCode as string
menuCode = cboDetails.selectedItem
Dim sqlCmd as New SqlCommand
sqlCmd.Connection = Connection.sqlConn
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandText = "prcGet_sub_menu_list"
sqlCmd.Parameter.Add("menuCode", SqlDbType.Char)
Dim sqlDA as New SqlDataAdapter()
sqlDA.SelectCommand = sqlCmd
Dim sqlDT as New DataTable
sqlDA.Fill(sqlDT)
This is the code that i have written and it gives me the error:
'Stored procedure prcGet_sub_menu_list expects parameter #sub_menu which was not supplied'.
Please give me some help on the same.
Regards,
George
You have to give the same parameter name and type like in your stored procedure.
sqlCmd.Parameters.Add(New SqlParameter("#sub_menu", SqlDbType.Char, 5)).Value = "Joe"
Geetha.
As the message implies, you should add a parameter named "sub_menu" in the same way as you're adding the "menuCode" parameter. You should probably also give it a value:
sqlCmd.Parameter.Add("sub_menu", SqlDbType.Char).Value = "Blah"
(Of course, I don't know what the correct type is, so Char is just an example.)
You need to use the correct parameter name
Something like
sqlCmd.Parameter.Add("#sub_menu", SqlDbType.Char)
And assign it a value
Something like
sqlCmd.Parameters("#sub_menu").Value = val
Have a look at SqlCommand.Parameters Property
and maybe Lesson 07: Using Stored Procedures
A couple things. Parameter should be Parameters. Also sqlCmd.Parameters.Add is deprecated so use sqlCmd.Parameters.AddWithValue:
sqlCmd.Parameters.AddWithValue("menuCode", menuCode)