comException is unhandled - vb.net-2010

Public Class Stock
Dim a As New Module1
Dim rs As ADODB.Recordset
Dim category, item As String
Private Sub Stock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a.Connect()
rs = New ADODB.Recordset
rs.Open("SELECT * FROM Stock", a.cn)'this line show error'
rs.MoveFirst()
While rs.EOF <> True
ComboBox1.Items.Add(rs.Fields("Item_Category").Value)
rs.MoveNext()
End While
End Sub

Related

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

Index Out Of Range Exception In VB.NET

When I run the following code, after the ColourAddition(selectedstyle,styleselection.getIndex)="Y" line, I get an error every time and when I run the code without the aforementioned line, the program is a success. Please help me figure it out
Public Class Form1
Dim dbcon As New OleDb.OleDbConnection
Dim DR As OleDb.OleDbDataReader
Dim dr1 As OleDb.OleDbDataReader
Dim com As New OleDb.OleDbCommand
Dim com1 As New OleDb.OleDbCommand
Dim ChkdItm As Integer
Dim NoOfStyles As Integer
Dim NoOfColours As Integer
Dim SelectedStyle As Integer
Dim temp As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dbcon.ConnectionString = "Provider= Microsoft.Ace.OLEDB.12.0; Data Source= CheckedListBox.accdb"
dbcon.Open()
com.CommandText = "Select * from ColourTable"
com1.CommandText = "Select * from StyleTable"
com1.Connection = dbcon
com.Connection = dbcon
DR = com.ExecuteReader
dr1 = com1.ExecuteReader
While dr1.Read
NoOfStyles += 1
End While
While DR.Read
ColoursClb.Items.Add(DR.GetString(1))
NoOfColours += 1
End While
SelectedStyle = StyleSelection.getIndex
End Sub
Public colourAddition(NoOfStyles, NoOfColours) As String
Private Sub AddColourBtn_Click(sender As Object, e As EventArgs) Handles AddColourBtn.Click
If ColoursClb.CheckedItems.Count > 0 Then
For Each ChkdItm In ColoursClb.CheckedIndices
colourAddition(SelectedStyle, StyleSelection.getIndex) = "Y"
Next
End If
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
StyleSelection.Show()
End Sub
End Class
The error is showing on the line where "Next" is written

GETTING ERROR when updating database in vb.NeT

I'M Creating A Database updater using vb.NET and database AS DATABASE1.SDF. I I WANTED TO Update the DATABASE IN DATAGridview dynamically. For UPDATING SQL COMMAND i'm USING A SQLCECOMMANDBUILDER BUT I'M GETTING A ERROR THERE AS "The DataAdapter.SelectCommand property needs to be initialized."
HERE IS MY CODE:
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Admin
Dim update As New SqlCeDataAdapter
' sql connection strings
Dim SQLCon As String = "Data Source=Database1.sdf"
Dim sqlstr As String = "Select * from Base_Plate "
Dim sqlstr1 As String = "Select * from Alloy "
Dim sqlstr2 As String = "Select * from Bead_Factor "
Dim sqlstr3 As String = "Select * from Difficulty_Factor "
Dim sqlstr4 As String = "Select * from Price_Factor "
' sql variable of base
Dim adapter As New SqlCeDataAdapter(sqlstr, SQLCon)
Dim ds As New DataSet()
' sql variable of alloy
Dim adapter1 As New SqlCeDataAdapter(sqlstr1, SQLCon)
Dim ds1 As New DataSet()
' sql variable of bead
Dim adapter2 As New SqlCeDataAdapter(sqlstr2, SQLCon)
Dim ds2 As New DataSet()
'sql variable of difficulty
Dim adapter3 As New SqlCeDataAdapter(sqlstr3, SQLCon)
Dim ds3 As New DataSet()
'sql variable of price
Dim adapter4 As New SqlCeDataAdapter(sqlstr4, SQLCon)
Dim ds4 As New DataSet()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
update.Update(ds)
LoadGrid()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Visible = False
LoginForm1.Visible = True
End Sub
Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadGrid()
Button2.Enabled = False
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Button2.Enabled = True
End Sub
Private Sub LoadGrid()
'************** base datagrid ********************
adapter.Fill(ds, "Base_Plate")
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Rows(0).Selected = True
'***************** alloy datagrid *********************
adapter1.Fill(ds1, "Alloy")
DataGridView2.DataSource = ds1.Tables(0)
DataGridView2.Rows(0).Selected = True
'***************** bead datagrid *********************
adapter2.Fill(ds2, "Bead_Factor")
DataGridView3.DataSource = ds2.Tables(0)
DataGridView3.Rows(0).Selected = True
'***************** difficulty datagrid *********************
adapter3.Fill(ds3, "Difficulty_Factor")
DataGridView4.DataSource = ds3.Tables(0)
DataGridView4.Rows(0).Selected = True
'***************** Price datagrid *********************
adapter4.Fill(ds4, "Price_Factor")
DataGridView5.DataSource = ds4.Tables(0)
DataGridView5.Rows(0).Selected = True
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
End Sub
You're doing it wrong. Firstly, as the error message states, you haven't set the SelectCommand of the data adapter that you're passing to the command builder constructor. Here's the relevant snippets from your code:
Dim update As New SqlCeDataAdapter
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
As you can see, nowhere do you provide a SQL SELECT statement so how is the command builder supposed to know how to build the other commands?
Secondly, even if the adapter was properly configured, this is wrong:
update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand
You DO NOT have to get the commands from the command builder and assign them to the properties of the data adapter. The only reason to do that is if you want to edit them, which you do not. The proper way to create a command builder is on the line immediately after you create the data adapter, e.g.
Dim myDataAdapter As New SqlCeDataAdapter(query, myConnection)
Dim myCommandBuilder As New SqlCeCommandBuilder(myDataAdapter)
That's it. You create it once and once only and then the data adapter will just work when you call Update. There's no creating a new one each time you save and there's no getting and assigning of commands.

HOW TO FILL COMBO BOX IN VB.NET

I have a code to fill my combo box but every time i close the form the list is being doubled, if i have a list from my database of English, Mathematics, Science after i close the form and open it again the list showing is now English, Mathematics, Science, English, Mathematics, Science
HERE IS THE CODE,
Call OpenDB()
cmd.CommandText = "select * from Subject"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read()
cmbSubject.Items.Add(dr("Subject Name"))
End While
dr.Close()
Call CloseDB()
The problem is not in the method you are using to bind the combobox. in each time it binds that's why you are getting duplicate records in the database. to avoid this please clear the combobox before each bind, like the following:
Call OpenDB()
cmbSubject.Items.Clear ' extra statement added to clear the item collection
cmd.CommandText = "select * from Subject"
cmd.Connection = conn
dr = cmd.ExecuteReader
While dr.Read()
cmbSubject.Items.Add(dr("Subject_Name"))
End While
dr.Close()
Call CloseDB()
If you need an alternate method for binding the combobox i will suggest you binding with Dataset
following is the example code for this :
Dim SQL As String= "select Subject_Name,Subject_code from Subject"
Dim adapter As New OdbcDataAdapter(SQL, conn) '<-- This function will Retrieve Data and Return as Dataset together with table name
Dim myData As New DataSet
myData.Fill(lobjDataSet, tblName)
cmbSubject.DataSource = ds_myData
cmbSubject.DataMember = "Subject"
cmbSubject.DataTextField = "Subject_Name"
cmbSubject.DataValueField = "Subject_code"
cmbSubject.DataBind()
myData .Dispose()
Public Class frmRegistration
Dim obj As New Entity.Registration
Dim bus As New Business.Registration
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
BindingSource1.EndEdit()
bus.Save(obj)
RefreshGrid()
MsgBox("Saved.")
End Sub
Sub RefreshGrid()
Dim dt1 As DataTable = bus.Select()
BindingSource2.DataSource = dt1
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = BindingSource2
End Sub
Private Sub Registration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RefreshGrid()
NewItem()
End Sub
Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNew.Click
NewItem()
End Sub
Sub ResetDis()
BindingSource1.DataSource = obj
End Sub
Sub NewItem()
obj = New Entity.Registration
ResetDis()
End Sub
Private Sub BindingSource2_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource2.CurrentChanged
obj = New Entity.Registration(CType(Me.BindingSource2.Current, DataRowView).Row)
ResetDis()
End Sub
End Class

syntax error in insert into statement in vb.net back end of access

i have created a mail tracker having a backend on ms-access 97-2003 format and all the code is fine except getting error on the said above please help on resolving the issue please refer to the screen shot and i'll provide the code below it
Imports System.Data.OleDb
Public Class Form1
Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\fsl.mdb;Persist Security Info=False;"
Dim cnn As OleDb.OleDbConnection
Dim now As Date = System.DateTime.Now()
Dim mydate As Date = now
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quit.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.cnn = New OleDb.OleDbConnection
rdate.Clear()
sdate.Clear()
orgi.Clear()
esub.Clear()
mno.Clear()
comap.Clear()
srno.Clear()
actby.Clear()
remark.Clear()
tim.Clear()
Label11.Visible = False
srno.Visible = False
End Sub
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
cnn.ConnectionString = dbprovider
cnn.Open()
Me.sdate.Text = now
Me.tim.Text = now
Dim sqlquery As String = "Insert into fsl([rdate],[sdate],[Originator],[Circ],[Subject],[Mno],[Comp],[Type],[subtype],[actby],[srno],[Status],[Agent],[remarks],[Tim])values (#rdate,#sdate,#Originator,#Circ,#Subject,#Mno,#Comp,#Type,#subtype,#actby,#srno,#Status,#Agent,#remarks,#Tim)"
Dim cmd As OleDbCommand = New OleDbCommand(sqlquery, cnn)
cmd.Parameters.AddWithValue("#rdate", Me.rdate.Text)
cmd.Parameters.AddWithValue("#sdate", Me.sdate.Text)
cmd.Parameters.AddWithValue("#originator", Me.orgi.Text)
cmd.Parameters.AddWithValue("#circ", Me.circ.Text)
cmd.Parameters.AddWithValue("#subject", Me.esub.Text)
cmd.Parameters.AddWithValue("#mno", Me.mno.Text)
cmd.Parameters.AddWithValue("#comp", Me.comap.Text)
cmd.Parameters.AddWithValue("#type", Me.type.Text)
cmd.Parameters.AddWithValue("#subtype", Me.subty.Text)
cmd.Parameters.AddWithValue("#actby", Me.actby.Text)
cmd.Parameters.AddWithValue("#srno", Me.srno.Text)
cmd.Parameters.AddWithValue("#status", Me.status.Text)
cmd.Parameters.AddWithValue("#agent", Me.agent.Text)
cmd.Parameters.AddWithValue("#remarks", Me.remark.Text)
cmd.Parameters.AddWithValue("#tim", Me.tim.Text)
cmd.ExecuteNonQuery()
cnn.Close()
MsgBox("Record Added Successfully")
rdate.Clear()
sdate.Clear()
orgi.Clear()
esub.Clear()
mno.Clear()
comap.Clear()
srno.Clear()
actby.Clear()
remark.Clear()
tim.Clear()
End Sub
Private Sub type_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles type.LostFocus
If type.SelectedItem = "E-Bridge" Then
Label11.Visible = True
srno.Visible = True
Else
Label11.Visible = False
srno.Visible = False
End If
End Sub
Private Sub subty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles subty.LostFocus
If subty.SelectedItem = "E-Bridge" Then
Label11.Visible = True
srno.Visible = True
ElseIf subty.SelectedItem = "Churn" Then
Label11.Visible = True
srno.Visible = True
Else
Label11.Visible = False
srno.Visible = False
End If
End Sub
End Class
You are missing the "VALUES" keyword.
Dim sqlquery As String = "Insert into fsl Values ([rdate],[sdate], ...
Take a look at this website:
http://www.w3schools.com/sql/sql_insert.asp