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.
Related
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
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
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();
I am trying to execute a stored procedure and place the information in a datagrid(without using the .net wizard) i want to do it manually. Using ado.net i believe. I am using vb.net and asp.net
Public cmd As New SqlCommand()
Public saoda As New SqlDataAdapter(cmd)
Public conn As New SqlConnection(" ")
Dim saods As New DataSet
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("#yeartoget", DropDownList1.SelectedValue)
cmd.CommandText = "casof"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
GridView1.DataSource = saods
saoda.FillSchema(saods, SchemaType.Mapped)
GridView1.DataBind()
conn.Close()
End Sub
End Class
vvvvv
Dim pctofsales As New DataColumn
pctofsales = New DataColumn("PCTofsales", GetType(Decimal))
pctofsales.Expression = "IIF([YEsales] = 0, 0, [ASOFSales] / [YEsales])"
saods1.Tables("salesasoftable").Columns.Add(pctofsales)
You don't need to bind a DataSet to a GridView - a SqlDataReader will suffice. That is, you could use the following:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("#yeartoget", DropDownList1.SelectedValue)
cmd.CommandText = "casof"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
GridView1.DataSource = reader
GridView1.DataBind()
conn.Close()
End Sub
However, if you want/need to use a DataTable, that's no big deal, either. The following snippet loads a reader into a DataTable:
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("#yeartoget", DropDownList1.SelectedValue)
cmd.CommandText = "casof"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
Dim myTable As DataTable = New DataTable()
myTable.Load(reader)
GridView1.DataSource = dt
GridView1.DataBind()
conn.Close()
Happy Programming!
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