0

I am trying to save my scale configurations in the database so first I have to check if it exists in the database.and I am using the code below to do that . however when there is no data in the database I will get an error "No row at position 0" or when the scale number is not there i will get the same error. can anyone help?

Dim dTable As DataTable = New DataTable
        Dim Sql As String = "select ScaleNumber from Scale where ScaleNumber = '" & cmbScaleNumber.Text & "' "
        'Dim cmd As New OleDbCommand(iSql, Conn)
        Dim da As New OleDbDataAdapter(Sql, Conn)
        da.Fill(dTable)

        If dTable.Rows.Count = 0 Then

            If dTable.Rows(0)("ScaleNumber") = cmbScaleNumber.Text Then
                MsgBox("The Scale is configured")
                Exit Sub
            End If
        Else
            MsgBox("Nice one")
        End If
3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by aksay
0

your checking for IF dTable.Rows.Count = 0
if count is 0 means there is no data in datatable... how will you get the value?
I think it should be IF NOT dTable.Rows.Count = 0

-1

Imports System.Data.OleDb
Public Class salesorderdetails
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='tables.accdb'")
Dim apdt As New OleDbDataAdapter("select * from Customer_details", con)
Dim sod1 As New OleDbDataAdapter("select * from Sales_Order_Details", con)
Dim apdt1 As New OleDbDataAdapter("select * from Sales_Order", con)
Dim dt As New DataTable("Customer_details")
Dim dt1 As New DataTable("Product_details")
Dim dt2 As New DataTable("Product_details")
Dim dt5 As New DataTable("Product_details")
Dim dt6 As New DataTable("product_details")
Dim dt7 As New DataTable("product_details")
Dim dt3 As New DataTable("Customer_details")
Dim sod As New DataTable("Sales_Order_Details")
Dim dt4 As New DataTable("saletemp")
Dim so As New DataTable("Sales_Order")
Dim rw As Integer = 0
Dim str As String
Sub display()
TextBox2.Text = so.Rows(rw).Item(0)
ComboBox1.Text = so.Rows(rw).Item(1)
DateTimePicker1.Text = so.Rows(rw).Item(2)
TextBox1.Text = so.Rows(rw).Item(3)
' ComboBox2.Text = sod.Rows(rw).Item(1)
'TextBox4.Text = sod.Rows(rw).Item(3)
'TextBox5.Text = sod.Rows(rw).Item(2)
' DataGridView1.Rows(rw).Selected = True
'DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub salesorderdetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
apdt1.Fill(so)
sod1.Fill(sod)
DataGridView1.DataSource = so
DataGridView1.DataSource = sod
display()
sod.Clear()

TextBox1.Enabled = False
TextBox3.Enabled = False
TextBox4.Enabled = False
TextBox2.Enabled = False
con.Open()
Dim da As New OleDbDataAdapter("Select Customer_name from Customer_details", con)
da.Fill(dt)
ComboBox1.Items.Clear()
Dim i As Integer
If dt.Rows.Count > 0 Then
For i = 0 To dt.Rows.Count - 1
ComboBox1.Items.Add(dt.Rows(i).Item("Customer_name"))

Next
End If
dt.Clear()
Dim da1 As New OleDbDataAdapter("Select Product_name from Product_details", con)
da1.Fill(dt1)
ComboBox2.Items.Clear()
Dim h As Integer
If dt1.Rows.Count > 0 Then
For h = 0 To dt1.Rows.Count - 1
ComboBox2.Items.Add(dt1.Rows(h).Item("Product_name"))

Next
End If
dt1.Clear()

sod1.Fill(sod)
DataGridView1.DataSource = sod

End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
MsgBox("Invalid...!! You Can Not Enter Text Here.")
e.KeyChar = ""
End If
End Sub

Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
MsgBox("Invalid...!! You Can Not Enter Text Here.")
e.KeyChar = ""
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
customerdetails.Show()
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim da2 As New OleDbDataAdapter("select Price from Product_details where Product_name='" & (ComboBox2.SelectedItem) & "'", con)
da2.Fill(dt2)
TextBox4.Text = dt2.Rows(rw).Item(0)
Dim da3 As New OleDbDataAdapter("select Profit from Product_details where Product_name='" & (ComboBox2.SelectedItem) & "'", con)
da3.Fill(dt5)
Dim p As Integer
p = dt5.Rows(rw).Item(0)
TextBox4.Text = ((TextBox4.Text * p) / 100)
TextBox4.Text = (dt2.Rows(rw).Item(0)) + (TextBox4.Text)
Dim da4 As New OleDbDataAdapter("select qty from Product_details where Product_name='" & (ComboBox2.SelectedItem) & "'", con)
da4.Fill(dt6)
TextBox3.Text = dt6.Rows(rw).Item(0)

Dim da5 As New OleDbDataAdapter("select product_description from Product_details where Product_name='" & (ComboBox2.SelectedItem) & "'", con)
da5.Fill(dt7)
Label14.Text = dt7.Rows(rw).Item(0)
dt2.Clear()
dt3.Clear()
dt6.Clear()
dt7.Clear()
If ComboBox2.Text = "" Then
TextBox4.Text = ""
End If
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
ComboBox1.Text = ""
ComboBox2.Text = ""
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Label14.Text = ""
Label11.Text = ""
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim da3 As New OleDbDataAdapter("select Mobile from Customer_details where Customer_name='" & (ComboBox1.SelectedItem) & "'", con)
da3.Fill(dt3)
TextBox1.Text = dt3.Rows(rw).Item(0)
dt3.Clear()
If ComboBox1.Text = "" Then
TextBox1.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MsgBox("Please Select Product Name And Give Quntity...")
Else
Dim price As Integer
Dim qty As Integer
Dim total As String
price = TextBox4.Text
qty = TextBox5.Text
total = price * qty
Label11.Text = "Rs." + total
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim da3 As New OleDbDataAdapter("select Customer_id from Customer_details where Customer_name='" & (ComboBox1.SelectedItem) & "'", con)
Dim dtx As New DataTable("Customer_details")
da3.Fill(dtx)
Dim cmd As New OleDbCommand
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.CommandText = "insert into Sales_Order values('" & TextBox2.Text & "','" & dtx.Rows(0).Item(0).ToString() & "','" & DateTimePicker1.Text & "','" & TextBox1.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Your Transection is Succeddfully Done...")
Button5.PerformClick()
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
ComboBox1.Text = ""
Dim d As New OleDbDataAdapter("select * from Sales_Order", con)
d.Fill(so)
Dim x As Integer = so.Rows.Count - 1

TextBox2.Text = so.Rows(x).Item(0) + 1

End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click


str = "insert into `Sales_Order_Details`(`Sales_Order_id`,`Product_name`,`Qty`,`Price`,`Total Amount`) values('" & Val(TextBox2.Text) & "','" & ComboBox2.Text & "','" & Val(TextBox5.Text) & "','" & Val(TextBox4.Text) & "','" & (Label11.Text) & "')"
Dim cmd As New OleDbCommand(str, con)
cmd.ExecuteNonQuery()
sod.Clear()
sod1.Fill(sod)
DataGridView1.DataSource = sod
MsgBox("Successfull.....")
ComboBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Label14.Text = ""
Label11.Text = ""
TextBox5.Text = ""
TextBox3.Enabled = False

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If rw = 0 Then
MsgBox("First record", MsgBoxStyle.Information)
Else
rw -= 1
display()
End If
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If rw = sod.Rows.Count - 1 Then
MsgBox("Last record", MsgBoxStyle.Information)
Else
rw += 1
display()
End If
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If rw = sod.Rows.Count - 1 Then
MsgBox("Last Record", MsgBoxStyle.Information)
Else
rw = sod.Rows.Count - 1
display()
End If
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If rw = 0 Then
MsgBox("first record", MsgBoxStyle.Information)
Else
rw = 0
display()
End If
End Sub
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
rw = e.RowIndex
display()
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Dim str As String
str = "DELETE FROM `Sales_Order` WHERE (`Sales_Order_id`=" & Val(TextBox2.Text) & ")"
Dim cmd As New OleDbCommand(str, con)
cmd.ExecuteNonQuery()
so.Clear()
MsgBox("Deleted")


End Sub
End Class

Votes + Comments
Don't hijack, use CODE tags, explain what's wrong and where.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.