display table in gridview for website in vb.net - asp.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

Related

Getting the data from the program to GridView in VB.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

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

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

Upload Image in ASP.NET VB

Is there any way an image can be uploaded with ASP.NET with VB?
I need an "upload" button on the ASP.NET side.
I already have an image field in the table in SQL server 2008.
Is there any way I can do this?
Add a fileupload control with:
<asp:FileUpload runat="server" id="FileUpload1" />
Add an upload button, and place this in the button click event handler:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim bldgIDNum As Int32 = FormView_Building.SelectedValue
If FileUpload1.PostedFile IsNot Nothing AndAlso FileUpload1.PostedFile.FileName <> "" Then
Dim imageSize As Byte() = New Byte(FileUpload1.PostedFile.ContentLength - 1) {}
Dim uploadedImage__1 As HttpPostedFile = FileUpload1.PostedFile
uploadedImage__1.InputStream.Read(imageSize, 0, CInt(FileUpload1.PostedFile.ContentLength))
' Create SQL Connection
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
' Create SQL Command
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Table (PrimaryKey,ImageData) VALUES (#PrimaryKey,#ImageData)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim PrimaryKey As New SqlParameter("#PrimaryKey", SqlDbType.Int, 32)
PrimaryKey.Value = (however you want to get it)
Dim UploadedImage__2 As New SqlParameter("#ImageData", SqlDbType.Image, imageSize.Length)
UploadedImage__2.Value = imageSize
cmd.Parameters.Add(UploadedImage__2)
con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
con.Close()
If result > 0 Then
lblMessage.Text = "File Uploaded"
End If
End If
ListView_BldgImages.DataBind()
End Sub
The database column ImageData should be varbinary(max)
Create a handler called Handler_Image.ashx with the following content:
Imports System
Imports System.Web
Imports System.Configuration
Imports System.Data.SqlClient
Public Class Handler_Image : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
' Create SQL Command
Dim cmd As New SqlCommand()
cmd.CommandText = "Select ImageData from Table where PrimaryKey =#PrimaryKey"
cmd.CommandType = System.Data.CommandType.Text
cmd.Connection = con
Dim ID As New SqlParameter("#PrimaryKey", System.Data.SqlDbType.Int)
ID.Value = context.Request.QueryString("PrimaryKey")
cmd.Parameters.Add(ID)
con.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
dReader.Read()
context.Response.BinaryWrite(DirectCast(dReader("ImageData"), Byte()))
dReader.Close()
con.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Display the image with an image control with the following property:
ImageUrl='<%# "Handler_Image.ashx?PrimaryKey=" & Eval("PrimaryKey")%>'
Replace the connectionstring, the table name, and the primary key to suit your application

Resources