• 0

[VB.NET] Count rows


Question

Hi,

I'm working on commercial app that deals with invoices, so every invoices has to increase its/own number regard to number of rows within MS Access DB and put the value to the TextBox6 .... i tried this below but it only counts one row within DB and i always start with No. 2 in the field invoice No. no matter i have even thousends of rows //how it could be ... i'm very confused

? ?Private Sub Faktura_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter

 ? ? ? ?oledbcon.Open()
 ? ? ? ?Dim sqlNew As New OleDbCommand("SELECT IDNum FROM Fakturi", oledbcon)
 ? ? ? ?sqlNew.ExecuteNonQuery()
 ? ? ? ?Try
 ? ? ? ? ? ?da.Fill(ds, "Table1")
 ? ? ? ?Catch ex As Exception
 ? ? ? ? ? ?MessageBox.Show(ex.Message)
 ? ? ? ?End Try
 ? ? ? ?TextBox6.Text = Val(ds.Tables("Table1").Rows.Count.ToString + 1)
 ? ? ? ?oledbcon.Close()
 ? ?End Sub

Thanks in advance

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
Hi,

I'm working on commercial app that deals with invoices, so every invoices has to increase its/own number regard to number of rows within MS Access DB and put the value to the TextBox6 .... i tried this below but it only counts one row within DB and i always start with No. 2 in the field invoice No. no matter i have even thousends of rows //how it could be ... i'm very confused

   Private Sub Faktura_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter

        oledbcon.Open()
        Dim sqlNew As New OleDbCommand("SELECT IDNum FROM Fakturi", oledbcon)
        sqlNew.ExecuteNonQuery()
        Try
            da.Fill(ds, "Table1")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        TextBox6.Text = Val(ds.Tables("Table1").Rows.Count.ToString + 1)
        oledbcon.Close()
    End Sub

Thanks in advance

584790404[/snapback]

This should work:

TextBox6.Text = ds.Tables("Table1").Rows.Count.ToString()

Link to comment
Share on other sites

  • 0
This should work:

TextBox6.Text = ds.Tables("Table1").Rows.Count.ToString()

584790491[/snapback]

nope ... it doesn't work ... still return 1 row only .... instead hundreds

there is another solution for this ... please

Link to comment
Share on other sites

  • 0

Could you provide the initializer for da?

From you code, it appears you're executing a SELECT query, but not using the results (ExecuteNonQuery). Then you use an object not defined in your snippet to fill the DataSet. More code is needed.

Also, as weenur showed you, you can't add the integer 1 to a string value (well, it will just concatinate it). The Count property will get you what you need.

Link to comment
Share on other sites

  • 0
Maybe I'm overlooking something, but why don't you just use the SQL COUNT function?

SELECT COUNT(IDNum) FROM Fakturi

584790650[/snapback]

Ok, maybe your idea is good enough to resolve the problem, but how can i apply the count to TextBox6's text property .... can you give me one short example ... please

@ Sn1p3t:

More code is needed.

I have no ideas anymore except the D-FENS's one ... so if you are willing let's put something more about the code that i miss ... thanks in advance

Link to comment
Share on other sites

  • 0

ok the below code is very rough, i haven't declared anything or set connection strings etc...but the core of it is there

conn.open()
cmd.connection = conn
cmd.commandtext = "SELECT COUNT(IDNum) FROM Fakturi"
intCount = cmd.ExecuteScalar()
conn.close()
TextBox6.Text = intCount.ToString()

Link to comment
Share on other sites

  • 0
ok the below code is very rough, i haven't declared anything or set connection strings etc...but the core of it is there

conn.open()
cmd.connection = conn
cmd.commandtext = "SELECT COUNT(IDNum) FROM Fakturi"
intCount = cmd.ExecuteScalar()
conn.close()
TextBox6.Text = intCount.ToString()

584790766[/snapback]

Thanks to you i got this (already) workable code:

Private Sub Faktura_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oledbcon.Open()
        Dim sqlNew As New OleDbCommand("SELECT COUNT(broj) FROM Fakturi", oledbcon)
        sqlNew.ExecuteScalar()
        Dim superiska = sqlNew.ExecuteScalar()
        Try
            da.Fill(ds, "Fakturi")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        TextBox6.Text = superiska.ToString() + 1
        oledbcon.Close()
    End Sub

Thank you very much

Link to comment
Share on other sites

  • 0
no worries

584790798[/snapback]

Sorry cause i continue with another subject but i thought .... if i may ...lol

Means, can i continue with this manner getting string from DB and load images into PictureBox .... eg. PicBox1.image = FromFile(variable.ToString()) ... i tried everything but it seems to me i was wrong all the time ... lol

my path within db is like: folderPic/image1.jpg, folderPic/image2.jpg and so on

Thanks in advance

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.