Getting the data from the program to GridView in VB.NET - asp.net

I am trying to post the data from the program, that is Id, FirstName and LastName to a grid view. I don't the grid at all. I crated the grid. Thanks for the help.
What I have tried:
If results.Rows.Count > 1 Then
Dim ds As DataSet = GetDataItem(_Id, _FirstName, _LastName)
If (ds.Tables.Count > 0) Then
GridView1.DataSource = ds
GridView1.DataBind()
End If
End If
It doesn't work. I don't see the table when I run. Can anyone help? Thanks.

please go through: "GridView Control In VB.NET"
Private sqlDataAdapter As SqlDataAdapter
Private sqlConnection As SqlConnection
Private dataSet As New DataSet()
Private sqlCommand As New SqlCommand()
Public Sub BindData()
sqlConnection = New SqlConnection("YourConnectionString")
sqlCommand.CommandText = "Select _Id, _FirstName, _LastName from YourTableName"
sqlCommand.Connection = sqlConnection
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(dataSet)
sqlConnection.Open()
sqlCommand.ExecuteNonQuery()
GridView1.DataSource = dataSet
GridView1.DataBind()
sqlConnection.Close()
End Sub

Related

ADO.net ASP.net How to Fill datatable?

I am new to this....How do I correctly fill this datatable ? The editor does not like this code, it gives me squiggles beneath the last occurrence of conn and Fill
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class WebForm4
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connstring As String
connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\CompanyData.mdb"
Dim conn As OleDbConnection
conn = New OleDbConnection(connstring)
conn.Open()
Dim strSql As String
strSql = "select * from Company"
Dim cmd As SqlCommand
cmd = New SqlCommand(strSql, conn)
Dim da As SqlDataAdapter
da = New SqlDataAdapter
da.SelectCommand = cmd
Dim dt As DataTable
dt = New DataTable
da.Fill(dt, "Company")
End Sub
You are using an OleDb to use an MS-Access database. You should use OleDb classes for everything. You create an SqlCommand and a SqlDataAdapter, these classes work for Sql Server but not with OleDb.
Dim connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\CompanyData.mdb"
Dim strSql = "select * from Company"
Using conn = New OleDbConnection(connstring)
Using cmd = New OleDbCommand(strSql, conn)
conn.Open()
Using da = New OleDbDataAdapter
da.SelectCommand = cmd
Dim dt = New DataTable
da.Fill(dt, "Company")
End Using
End Using
I have changed a bit your code to show how to use the Using Statement. This statement is very important to keep your code clean because it closes and disposes every disposable object declared in the using line.

to filter the rdlc report data according to parameter passed in vb.net

Try
Dim ed As New Editor()
Using con As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=LMS ;Integrated Security=True")
con.Open()
Dim cmd As New SqlCommand("select * from Editor", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ed, "DataTable1")
con.Close()
End Using
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim parReportParam1 As New ReportParameter("Parameter1", Me.txtLan.Text.ToString())
Me.ReportViewer1.LocalReport.SetParameters(New ReportParameter() {
parReportParam1
})
Dim datasource As New ReportDataSource("Editor", ed.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
Me.ReportViewer1.LocalReport.Refresh()
Catch ex As Exception
End Try
I am not able to get any data from my data set it is showing empty report viewer after adding parameter to report.rdlc
Protected Sub bSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bSubmit.Click
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
Dim ed As New Editor()
Using con As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=LMS ;Integrated Security=True")
con.Open()
Dim cmd As New SqlCommand("select * from Editor where ([Language] = #Lang)", con)
cmd.Parameters.AddWithValue("#Lang", txtLan.Text)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ed, "DataTable1")
con.Close()
End Using
Dim datasource As New ReportDataSource("Editor", ed.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End Sub
End Class

display table in gridview for website in vb.net

I am new to vb.net and I want to display an Access table in gridview using vb.net in an ASP.NET secnario.
Public Class Form1
Public Sub New()
InitializeComponent()
BindGrid()
End Sub
Private Sub BindGrid()
Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass#123"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
End Class
Can someone please help me?
To use a ACCESS database you must change your connection string.
Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass#123"
To
http://www.connectionstrings.com/access/
Also
Using ds As New DataSet()
sda.Fill(ds)
GridView1.DataSource = ds.Table(0)
GridView1.DataBind()
End Using

Not able to fetch data from sql database in asp.net web application using vb

I am using the below code to fetch data from SQL, I am not getting any error but code is not working on button click
Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
Dim command As New SqlCommand(strSQL, connection)
connection.Open()
reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
GridView1.DataSource = reader
End While
'end connection and using close
I think you have to modify your code as follows,
Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
Dim command As New SqlCommand(strSQL, connection)
connection.Open()
reader As SqlDataReader = command.ExecuteReader()
DataTable dt = new DataTable();
dt.Load(reader);
GridView1.DataSource = dt;
GridView1.DataBind();
You need to DataBind your GridView after providing the datasource:
GridView1.DataBind();

Ado.net ExecuteReader giving duplication while binding with datagrid

I am using below mentioned Ado.net function and resultset bind with grid view, however I am getting the duplicate rows in the resultset.
Please help me out.
Thanks
Private _products As New List(Of Product)
Public Property Products As List(Of BusinessObjects.Product)
Get
Return _products
End Get
Set(ByVal value As List(Of BusinessObjects.Product))
_products = value
End Set
End Property
Public Function GetProductDetails() As List(Of Product)
Dim product As New BusinessObjects.Product
Using connection As New SqlConnection
connection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
connection.Open()
Using Command As New SqlCommand("select * from T_product", connection)
Dim rdr As SqlDataReader
rdr = Command.ExecuteReader
While rdr.Read()
product.ProductID = rdr("ProductID")
product.ProductName = rdr("ProductName")
Products.Add(product)
End While
GridView1.DataSource = Products
GridView1.DataBind()
End Using
End Using
Return Products
End Function
You should make Dim product As New BusinessObjects.Product initialization inside while reading from SqlDataReader instance
Set(ByVal value As List(Of BusinessObjects.Product))
_products = value
End Set
End Property
Public Function GetProductDetails() As List(Of Product)
Using connection As New SqlConnection
connection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
connection.Open()
Using Command As New SqlCommand("select * from T_product", connection)
Dim rdr As SqlDataReader
rdr = Command.ExecuteReader
While rdr.Read()
Dim product As New BusinessObjects.Product
product.ProductID = rdr("ProductID")
product.ProductName = rdr("ProductName")
Products.Add(product)
End While
GridView1.DataSource = Products
GridView1.DataBind()
End Using
End Using
Return Products
End Function
The problem is you are updating and adding same product every time. Create product object inside the While loop as below.
Public Function GetProductDetails() As List(Of Product)
Using connection As New SqlConnection
connection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
connection.Open()
Using Command As New SqlCommand("select * from T_product", connection)
Dim rdr As SqlDataReader
rdr = Command.ExecuteReader
While rdr.Read()
Dim product As New BusinessObjects.Product ' product object create here
product.ProductID = rdr("ProductID")
product.ProductName = rdr("ProductName")
Products.Add(product)
End While
GridView1.DataSource = Products
GridView1.DataBind()
End Using
End Using
Return Prod

Resources