Using SqlCommand Object in VB.NET - vb.net

Can I use two command object with one open connection in one procedure at VB.NET?

Yes you can. As long as you don't close the connection between commands, it'll work fine.
This is a C# example, but I'm sure you can work it out:
using (SqlConnection cn = new SqlConnection("Connection String"))
{
SqlCommand cmd1 = new SqlCommand("Command1", cn);
cmd1.CommandType = CommandType.StoredProcedure;
SqlCommand cmd2 = new SqlCommand("Command2", cn);
cmd2.CommandType = CommandType.StoredProcedure;
cn.Open();
// Execute cmd1
// Execure cmd2
}

Example; kinda pseudo but you should get the concept.
dim cnn as connection
dim cmd as command
dim cmd2 as command
dim str1 as string
dim str2 as string
cnn.open
cmd.connection = cnn
cmd.command = str1
cmd.execute
cmd2.connection = cnn
cmd2.command = str2
cmd2.execute
cnn.close

Related

Trying to use grid view with my data, but it outputs an error

Following is the code am using to bind the grid, can anyone please suggest me what am doing wrong in this snippet
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim adapter As SqlDataAdapter
Dim sql As String
con.ConnectionString = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234"
con.Open()
sql = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=****;Password=*****"
cmd.CommandText = "select * From demo_vb Where ID = '" & txtbox4.Text & "'"
adapter = New SqlClient.SqlDataAdapter
adapter.Fill(ds)
GridView1.ItemsSource = New DataView()
GridView1.DisplayMemberPath = "ID"
con.Close()
I get this error:
The SelectCommand property has not been initialized before calling 'Fill'
try following code:
SqlCommand cmd;
SqlConnection con = new SqlConnection("Data Source=SUNTECH-PC\\SQLEXPRESS;Initial Catalog=iSense;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("Select Picture from Images",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
I think this would help
Dim con As New SqlClient.SqlConnection("Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234")
Dim ds As New DataSet
Dim sql As New SqlCommand("select * From demo_vb Where ID = '" & txtbox4.Text & "'",con)
Dim adapter As New SqlDataAdapter(sql)
con.Open()
adapter.Fill(ds)
GridView1.datasource = ds.Tables(0)
GridView1.databind()
con.Close()

Sql wont run execute reader

I know that that ConfigurationManager.AppSettings("ADOConnectionString") gives the right location for my connection, and family_id is the number 6 (at family_id=6 I know I have the right data in my Product table) so why won't this run past execute reader?
Dim sqlConnection1 As New SqlConnection(ConfigurationManager.AppSettings("ADOConnectionString"))
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd.Parameters.AddWithValue("#family_id", family_id)
cmd.CommandType = CommandType.Text
sqlConnection1.Open()
reader = cmd.ExecuteReader()
You haven't attached the connection to your command. One way to do that is:
cmd.Connection = sqlConnection1
Another is to do it as part of the SqlCommand constructor:
Dim sql As String
Dim cmd As SqlCommand
Dim reader As SqlDataReader
sql = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=#family_id"
cmd = new SqlCommand(sql, sqlConnection1)

Is filling a DataTable necessary for just setting variables with 2 column values?

I am trying to improve performance of an application, I have a case where a common SPROC is being used but is it necessary to fill a DataTable just to set 2 variable values?
Is there anything more efficient?
Dim Conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB").ConnectionString)
Dim CmdUsers As SqlCommand = New SqlCommand("uspGetUsers", Conn)
CmdUsers.CommandType = CommandType.StoredProcedure
CmdUsers.Parameters.Add(New SqlParameter("#UserName", Session("UserID")))
Dim da As SqlDataAdapter = New SqlDataAdapter
Dim dtUserInfo As DataTable = New DataTable
da = New SqlDataAdapter(CmdUsers)
da.Fill(dtUserInfo)
isParent = dtUserInfo.Rows(0)("IsAdmin")
UserVal = dtUserInfo.Rows(0)("UserVal")
A SqlDataReader is the fastest way to read data from a query. Try the following:
Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB").ConnectionString)
conn.Open()
Using cmd As New SqlCommand("uspGetUsers", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#UserName", Session("UserID"))
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
isParent = reader("IsAdmin")
UserVal = reader("UserVal")
End While
End Using
End Using
End Using
You may need to parse the data to the correct types.
Also, note the use of Using to automatically dispose of the connection, command and reader objects: http://msdn.microsoft.com/en-GB/library/htd05whh.aspx

vb.net delete from access database

I'm trying to simply delete data from my access database on the click of a button..
But can't get it to work.
Dim Cmd2 As OleDbCommand
Dim SQL2 As String
Dim objCmd2 As New OleDbCommand
Dim Con2 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL2 = "DELETE FROM dreamware_db WHERE id='21'"
Cmd2 = New OleDbCommand(SQL2, Con2)
Con2.Open()
objCmd2 = New OleDbCommand(SQL2, Con2)
objCmd2.ExecuteNonQuery()
Con2.Close()
Can someone spot the error?
I write your method without errors:
Dim SQL As String
Dim objCmd As OleDbCommand
Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL = "DELETE FROM dreamware_db WHERE id='21'"
Con.Open()
objCmd = New OleDbCommand(SQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()

SQL Query help requested

I am trying to use the following query
Dim sqlQry As String = "SELECT * FROM tblTest where Name=#NM and Rank=#RN"
Then I fill my dataadapter by
Dim dAdt As New SqlDataAdapter(sqlQry, conStr)
But do not know where to put the parameters that I have set after where clause.
You can use the parameters like this:
Dim adapter As SqlDataAdapter = New SqlDataAdapter()
' Create the SelectCommand.
Dim command As SqlCommand = New SqlCommand("SELECT * FROM tblTest where Name=#NM and Rank=#RN", connection)
' Add the parameters for the SelectCommand.
command.Parameters.Add("#NM", SqlDbType.NVarChar, 15)
command.Parameters.Add("#RN", SqlDbType.NVarChar, 15)
adapter.SelectCommand = command
Check this MSDN Document
Create a parameter
SqlParameter param = new SqlParameter();
param.ParameterName = "#RN";
param.Value = inputCity;
Then add the param to your Sql Command.
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(param);
If I understand what you're asking, you need to create an instance of an SqlCommand and use your sqlQry with that. Then use SqlCommand.Parameters.Add() or SqlCommand.Parameters.AddWithValue() to add your parameters. Initialize your SqlDataAdapter with the SqlCommand instead of the String you've created.
Using connection As New SqlConnection(conStr)
Dim command As New SqlCommand(sqlQry, connection)
command.Parameters.Add("#NM", SqlDbType.NVarChar, 100).Value="Your Value"
command.Parameters.AddWithValue("#RN", "Your Value")
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(dataSet)
Return dataSet
End Using