reportview using sqldataadapter - vb.net

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()

Related

Why is my VB program throwing an exception complaining that a OleDbDataReader is closed, when it should absolutely be open?

I am trying to use OleDB and SQLBulkCopy to pull data off an excel spreadsheet, into an SQL database. When I try and run my code, I get the following exception, triggered by
"bulkCopy.WriteToServer(objDR)" (which is also described in the question title): exception data
OleDB successfully reads the excel sheet which is then populated properly in the DataGridView I created within a WinForm for debugging purposes.
Below is a snippet of the relevant code.
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\damon\Everyone\sheet1erictest.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim sheet As String = "Sheet5$"
Dim expr As String = "SELECT * FROM [" + sheet + "]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "SERVER=SqlDEV;DATABASE=Freight;Integrated Security=True"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "dbo.test"
Try
objDR = objCmdSelect.ExecuteReader
Dim dt = New DataTable()
dt.Load(objDR)
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Your reader object is being used by the DataTable, so comment out those lines and run them separately later with a new instance:
objDR = objCmdSelect.ExecuteReader
'Dim dt = New DataTable()
'dt.Load(objDR)
'DataGridView1.AutoGenerateColumns = True
'DataGridView1.DataSource = dt
'DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)

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()

Select from sql in vb and display in aspx page

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

how to add list items in listbox in vb.net

i am trying to add list items in listbox by the below code
if i am wrong please correct me ..
the below code is giving weird output.
Try
Dim adapter As New OleDbDataAdapter
con = New OleDbConnection(connectionString)
con.Open()
Dim dt As New DataTable
Dim ds As New DataSet
sqlstr = "select * from partyinfo "
ds.Tables.Add(dt)
adapter.SelectCommand = New OleDbCommand(sqlstr, con)
adapter.Fill(dt)
lbpartylink.DataSource = ds.Tables(0)
lbpartyunlink.ValueMember = "partyid"
lbpartyunlink.DisplayMember = "partyname"
con.Close()
Catch ex As Exception
MsgBox("error found")
End Try
my fault i wrote
lbpartylink.DataSource = ds.Tables(0)
in place of
lbpartyulink.DataSource = ds.Tables(0)

How to refresh datagridview without refresh button?

How can I automatically refresh and view the new current datagridview after add or delete?
what code should I put after "msgbox" to view the current data?
Private Sub add()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim sSQL As String = String.Empty
Try
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "INSERT INTO course ( code, description)"
sSQL = sSQL & " VALUES (#cod, #des)"
cmd.CommandText = sSQL
cmd.Parameters.Add("#cod", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtcode.Text)) > 0, Me.txtcode.Text, DBNull.Value)
cmd.Parameters.Add("#des", OleDbType.VarChar).Value = IIf(Len(Trim(Me.txtdescription.Text)) > 0, Me.txtdescription.Text, DBNull.Value)
cmd.ExecuteNonQuery()
MsgBox("Data has been save.")
conn.Close()
Catch ex As Exception
MessageBox.Show("Already exist")
End Try
End Sub
You should bind your DataGridView to an appropriate DataSource, e.g. a DataTable.
Then, instead of inserting the data manually in the database, you could use a SqlDataAdapter and the DataTable your DataGridView is bound to.
Here's a simple example:
Dim cons = New SqlConnectionStringBuilder() With
{
.DataSource = "your_server",
.InitialCatalog = "your_db",
.UserID = "your_user",
.Password = "your_password"
}.ConnectionString
Dim con = New SqlConnection(cons)
con.Open()
' create a SqlDataAdapter and provide the SELECT command '
Dim adapter = New SqlDataAdapter()
Dim cb = New SqlCommandBuilder(adapter)
adapter.SelectCommand = New SqlCommand("SELECT code, description FROM course", con)
' the INSERT command can be generated '
adapter.InsertCommand = cb.GetInsertCommand()
' fill a new DataTable with data from database '
Dim dt = New DataTable()
adapter.Fill(dt)
' create a Form with DGV and Insert-Button '
Dim f = New Form()
Dim dgv = New DataGridView() With
{
.DataSource = dt,
.Dock = DockStyle.Fill
}
Dim addButton = New Button() With
{
.Text = "Add new",
.Dock = DockStyle.Bottom
}
Dim i = 0
AddHandler addButton.Click, Function(s, o)
' we insert the new data directly into the DataTable '
dt.Rows.Add(New Object() {"Some","Text"})
' and let the SqlDataAdapter handle the insert '
adapter.Update(dt)
End Function
f.Controls.Add(addButton)
f.Controls.Add(dgv)
f.ShowDialog()
Since the new data is written directly into the DataTable, the DataGridView is updated immediately.
Of course this is even easier when you use TableAdapters.