vb.net delete from access database - vb.net

I'm trying to simply delete data from my access database on the click of a button..
But can't get it to work.
Dim Cmd2 As OleDbCommand
Dim SQL2 As String
Dim objCmd2 As New OleDbCommand
Dim Con2 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL2 = "DELETE FROM dreamware_db WHERE id='21'"
Cmd2 = New OleDbCommand(SQL2, Con2)
Con2.Open()
objCmd2 = New OleDbCommand(SQL2, Con2)
objCmd2.ExecuteNonQuery()
Con2.Close()
Can someone spot the error?

I write your method without errors:
Dim SQL As String
Dim objCmd As OleDbCommand
Dim Con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Privat\dreamware\dreamware_db.mdb")
SQL = "DELETE FROM dreamware_db WHERE id='21'"
Con.Open()
objCmd = New OleDbCommand(SQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()

Related

How can I insert an image from SQL Server onto a button in VB.net?

I am having issues assigning an image to every button according to my database.
My current code:
Dim strsql As String
Dim ImgSQl As Image
Dim con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSQl = myreader("Image")
con.Close()
btn1.Image = ImgSQl
This is the error I'm getting:
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'
All I had to do was dim as Byte, also adding "()" is really important for it to work. Hope answering my own question helps a VB.net beginner out there.
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=#ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("#ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
Dim ms As New MemoryStream(ImgSql)
btn1.Image = Image.FromStream(ms)
con.Close()
End Using

Crystal Report not showing data even though dataset contains data from MSAccess - what am I missing?

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim ssql As String
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= 'C:\Users\xxxx\xxxxx\xxxxxxxxe/xxxxxx.accdb'"
con.Open()
ssql = "SELECT * FROM tblTempProformaInvoice"
da = New OleDb.OleDbDataAdapter(ssql, con)
da.Fill(ds, "GetProformaInvoice")
con.Close()
MsgBox(ds.Tables("GetProformaInvoice").Rows(0).Item("spinvnr").ToString)
Dim rpt As New PFI
rpt.SetDataSource(ds)
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()
try
rpt.SetDataSource(ds.Tables(0))

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

How to configure multiple dataadapters?

The following code works ok.
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
But for some reason when I add additional dataadapters, as per the below, I get the following error
"Object reference not set to an instance of an object."
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim sql2 As String = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.[bill] From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" + TextBox1.Text + "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE s.[CLI] is NULL"
Dim comm2 As SqlCommand = New SqlCommand(sql2, conn)
Dim dataadapter2 As SqlDataAdapter = New SqlDataAdapter(comm2)
Dim sql3 As String = "SELECT s.[ID],s.[Site],s.[CLI],s.[CustomerName],s.[FromDate],s.[ToDate],s.[Quantity],s.[UnitCost],s.[TotalCost],s.[Description],s.[filenameonly],s.billingmonth as [CurrentBillingMonth], s.[bill] From [DaisyServices].[dbo].[" + TextBox1.Text + "] s LEFT JOIN [DaisyServices].[dbo].[DaisyServicesIndigo] i on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE i.[CLI] is NULL"
Dim comm3 As SqlCommand = New SqlCommand(sql3, conn)
Dim dataadapter3 As SqlDataAdapter = New SqlDataAdapter(comm3)
How should I configure multiple adapters on the same form?
Thanks

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")