What I am trying to do is modify this code so I can fetch a second datatable to append, how do I append results of a second datatable?
Dim myDataTable As New DataTable
Dim sqlConnection1 As New SqlConnection("connectionstring")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
Dim Family_id = lbFamilies.SelectedItem.Value
cmd.CommandText = "SELECT * FROM table WHERE Family_id=#Family_id"
cmd.Parameters.AddWithValue("#family_id", Family_id)
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
myDataTable.Load(reader)
sqlConnection1.Close()
lbProduct.DataSource = myDataTable
lbProduct.DataTextField = "product_name"
lbProduct.DataValueField = "product_id"
lbProduct.DataBind()
Assuming the two have the same schema, try this.
Replace:
lbProduct.DataSource = myDataTable
lbProduct.DataTextField = "product_name"
lbProduct.DataValueField = "product_id"
lbProduct.DataBind()
With this:
CType(lbProduct.DataSource,DataTable).Merge(myDataTable);
lbProduct.DataBind();
Related
Sorry for my noob question, But how can I add values in #id in a select command?
I'm using vb.net and MySQL
here is calling all the 'time' column, but I want to add a where in a query.
("Select time from dtr where id = #id") where will I put the parameters.addwithvalues command?
con = New MySqlConnection
con.ConnectionString = "server = localhost; userid = root; password=; database=mjb_payroll; SslMode=none"
Dim query As String = "Select time from dtr"
Dim cmd As New MySqlCommand(query, con)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()
adpt.Fill(tbl)
DataGridView1.DataSource = tbl
If your concern is just on adding parameters.addwithvalue you can add it after declaring your MySqlCommand:
Dim query As String = "Select time from dtr where id = #id"
Dim cmd As New MySqlCommand(query, con)
cmd.Parameters.AddWithValue("#id", id)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()
Consider the following code
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As New SqlDataAdapter(cmd)
Suppose that there exist a column name status I want to store the result of status in a variable.
P.S I am a beginner with VB.NET so please bear with me
You can execute the statement on a SqlDataReader or fill DataTable. An example with SqlDataReader is
Dim reader As SqlDataReader
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
reader = cmd.ExecuteReader()
Dim strStatus as String = ""
If reader.HasRows Then
reader.Read()
strStatus = reader.Item("status").ToString
End If
Here is the DataTable version
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName =#username and UserPass=#password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim dt As DataTable = New DataTable("TableA")
da.SelectCommand = cmd
da.Fill(dt)
Dim strStatus as String = ""
'you can process the DataTable in a for/for each loop or process a single row as follows
If dt.Rows.Count > 0 Then
strStatus = dt.Rows(0).Item("status").ToString()
End If
If status returns a single cell value(single row in status column) then you can use following method
Dim cmd As New SqlCommand("SELECT * FROM [UserDetail].[User] where UserName = #username and UserPass = #password", con)
cmd.Parameters.AddWithValue("#username", login_username.Text)
cmd.Parameters.AddWithValue("#password", hash_pass)
Dim status As String
status = IIf(IsDBNull(cmd.ExecuteScalar), "Not Available", cmd.ExecuteScalar)
'IsDBNull is used tocheck whether cmd.ExecuteScalar is null or what
'IIF() is used to handle null here, You can assign custom value for status variable if
'select returns null value
'If you dont need to use them its possible to write
' status = cmd.ExecuteScalar
See more about ExecuteScalar()
Just wanna select a word from database and display in aspx page
Backend
Using da As New SqlDataAdapter
con.Open()
cmd.CommandText = "SELECT value_en as value FROM tbl_language WHERE element_id = 'a1';"
da.SelectCommand = cmd
Dim dt As New DataTable
da.Fill(dt)
Dim WordValue As String = dt.Rows(0).Item(0)
End Using
in aspx page
<%=WordValue%>
whats wrong here?
Assign Connection to the command object and also convert item(0) into string
Using da As New SqlDataAdapter
con.Open()
cmd.CommandText = "SELECT value_en as value FROM tbl_language WHERE element_id = 'a1';"
cmd.Connection =con
da.SelectCommand = cmd
Dim dt As New DataTable
da.Fill(dt)
Dim WordValue As String = Convert.ToString(dt.Rows(0).Item(0))
End Using
or
Using da As New SqlDataAdapter( "SELECT value_en as value FROM tbl_language WHERE element_id = 'a1';",con)
Dim dt As New DataTable
da.Fill(dt)
Dim WordValue As String = Convert.ToString(dt.Rows(0).Item(0))
End Using
Trying to add a row to an Access 2010 database with VB 2013. Everything seems to work until the UPDATE statement when I get this error:
Update requires a valid InsertCommand when passed DataRow collection with new rows.
My code is:
Dim sqlConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\CLI_CRVM.accdb")
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ds As New DataSet1()
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "SELECT * FROM [extract] ;"
cmd.Connection = sqlConnection
sqlConnection.Open()
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
da.Fill(ds, "extract")
ds.Clear()
Dim newExtractRow As DataRow = ds.Tables("extract").NewRow()
newExtractRow("Field1") = "ABC123"
ds.Tables("Extract").Rows.Add(newExtractRow)
da.Update(ds, "Extract")
sqlConnection.Close()
Everything I've found so far seems to reference an SQL database, not OleDb connection.
You can probably use an OleDbCommandBuilder, like so
' your existing OleDbDataAdapter
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
' add the following lines:
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
For details, see
OleDbCommandBuilder Class
Working Example
For an Access table named extract with fields
ID - AutoNumber, Primary Key
Field1 - Text(255)
and data
ID Field1
-- -------
1 TEST999
the following VB.NET code will insert a second row into the table in the Access database
Using con As New OleDbConnection
con.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=C:\Users\Public\Database1.accdb;"
con.Open()
Using da As New OleDbDataAdapter("SELECT * FROM [extract]", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
Dim dt = New DataTable
da.Fill(dt)
Dim dr As DataRow = dt.NewRow
dr("Field1") = "ABC123"
dt.Rows.Add(dr)
da.Update(dt)
End Using
con.Close()
End Using
with the result
ID Field1
-- -------
1 TEST999
2 ABC123
The OleDbCommandBuilder object automatically (and invisibly) creates the INSERT, UPDATE, and DELETE commands based on the SELECT command that was supplied when the OleDbDataAdapter object was created.
You need to set the InsertCommand property
Dim da = New OleDb.OleDbDataAdapter(cmd.CommandText, sqlConnection)
da.InsertCommand = cmd
da.Fill(ds, "extract")
More info here
is there a way to make a reportviewer using sqldataadapter? is it possible?
It seems that I can't find a way to get it on datasource.
Dim con As SqlConnection = New SqlConnection("Data Source = pc11-pc\kim; Initial Catalog = mypos; User ID = sa; Password = 123")
Dim sqlstr As String = "Select * from Supplier"
Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlstr, con)
Dim dt As New DataTable
adp.Fill(dt)
'reportviewer1.datasources = dt
Here's the glimps of it...
Dim dt As DataTable = New DataTable
Dim conn As SqlConnection = New SqlConnection(connString)
Try
conn.Open()
Dim cmd As New SqlCommand(sql, conn)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
Catch ex As Exception
MessageBox.Show("Error")
Finally
conn.Close()
End Try
Dim ds As New ReportDataSource(dataSourceName, dt)
rViewer.LocalReport.DataSources.Clear()
rViewer.LocalReport.DataSources.Add(ds)
rViewer.LocalReport.Refresh()
here's my code sir
'TODO: This line of code loads data into the 'myposDataSet5.Product' table. You can move, or remove it, as needed.
Me.ProductTableAdapter.Fill(Me.myposDataSet5.Product)
Dim dt As DataTable = New DataTable
Dim conn As SqlConnection = New SqlConnection("Data Source = pc11-pc\kim; Initial Catalog = mypos; User ID = sa; Password = 123")
Try
conn.Open()
Dim cmd As New SqlCommand("Select * From Product", conn)
Dim adapter As New SqlDataAdapter(cmd)
adapter.Fill(dt)
Catch ex As Exception
MessageBox.Show("Error")
Finally
conn.Close()
End Try
'Dim ReportDataSource As DataTable = New DataTable
'Dim ds As New ReportDataSource(dt.TableName = "test", dt)
Dim ds As New Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt)
ReportViewer1.LocalReport.ReportPath = "D:\visual studio\mypos\mypos\Report3.rdlc"
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(ds)
ReportViewer1.LocalReport.Refresh()
Me.ReportViewer1.RefreshReport()