Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql count

sql count

Scheduled Pinned Locked Moved Database
database
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bapu2889
    wrote on last edited by
    #1

    hello i am new in sql and i have create one small application with one table which has six columns i want to check recores from sql database but it always shows nothing this is the code bellow in load event, all i want to do is if there is no data in sql database then it should shows "Empty Database" in Combo box but it's not working

    Private Sub LoadCboItems()

        dcIMS = GetDBConnection()
        cboDisp.Items.Clear()
       
        If Me.BindingContext(dsIMS, "IMS").Count = 0 Then   :confused:
            cboDisp.Text = "Empty Database"
            Exit Sub
        Else
            Dim MyReader As SqlDataReader = Nothing
            Try
                
                Dim MyData As New SqlCommand("SELECT \* FROM IMS", dcIMS)
                MyReader = MyData.ExecuteReader
                Do While MyReader.Read
                    If rdoID.Checked Then
    
                        cboDisp.Items.Add(MyReader.Item("InkNumber").ToString)
                        cboDisp.SelectedIndex = 0
    
                    ElseIf rdoName.Checked Then
    
                        cboDisp.Items.Add(MyReader.Item("InkName").ToString)
                        cboDisp.SelectedIndex = 0
    
                    End If
                Loop
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                MyReader.Close()
                dcIMS.Close()
                dcIMS.Dispose()
            End Try
        End If
    
    End Sub
    
    M N 2 Replies Last reply
    0
    • B bapu2889

      hello i am new in sql and i have create one small application with one table which has six columns i want to check recores from sql database but it always shows nothing this is the code bellow in load event, all i want to do is if there is no data in sql database then it should shows "Empty Database" in Combo box but it's not working

      Private Sub LoadCboItems()

          dcIMS = GetDBConnection()
          cboDisp.Items.Clear()
         
          If Me.BindingContext(dsIMS, "IMS").Count = 0 Then   :confused:
              cboDisp.Text = "Empty Database"
              Exit Sub
          Else
              Dim MyReader As SqlDataReader = Nothing
              Try
                  
                  Dim MyData As New SqlCommand("SELECT \* FROM IMS", dcIMS)
                  MyReader = MyData.ExecuteReader
                  Do While MyReader.Read
                      If rdoID.Checked Then
      
                          cboDisp.Items.Add(MyReader.Item("InkNumber").ToString)
                          cboDisp.SelectedIndex = 0
      
                      ElseIf rdoName.Checked Then
      
                          cboDisp.Items.Add(MyReader.Item("InkName").ToString)
                          cboDisp.SelectedIndex = 0
      
                      End If
                  Loop
              Catch ex As Exception
                  MsgBox(ex.Message)
              Finally
                  MyReader.Close()
                  dcIMS.Close()
                  dcIMS.Dispose()
              End Try
          End If
      
      End Sub
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      bapu2889 wrote:

      dcIMS = GetDBConnection() cboDisp.Items.Clear()

      I hate binding questions, what is dcIMS declared as? You are testing the count of the binding context, presumably tied to the form (me), is this what you want to test. I use datatables and have no problem with the counting of rows nor binding to a combo box. Did i mention I hate binding widgets!

      Never underestimate the power of human stupidity RAH

      B 1 Reply Last reply
      0
      • M Mycroft Holmes

        bapu2889 wrote:

        dcIMS = GetDBConnection() cboDisp.Items.Clear()

        I hate binding questions, what is dcIMS declared as? You are testing the count of the binding context, presumably tied to the form (me), is this what you want to test. I use datatables and have no problem with the counting of rows nor binding to a combo box. Did i mention I hate binding widgets!

        Never underestimate the power of human stupidity RAH

        B Offline
        B Offline
        bapu2889
        wrote on last edited by
        #3

        dcIMS is sql data connection cboDisp is combobox and what i want to do is when i start application so first it checks if there is any data in data base is it's there then it's shows the list of inks but if it's empty then i want to display "Empty Database" in combo box thanks for your rep. have a nice day

        B 1 Reply Last reply
        0
        • B bapu2889

          dcIMS is sql data connection cboDisp is combobox and what i want to do is when i start application so first it checks if there is any data in data base is it's there then it's shows the list of inks but if it's empty then i want to display "Empty Database" in combo box thanks for your rep. have a nice day

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          And your code work properly or not? If no do you have any error message or where is problem with code? Try to bind cboDisp with values from database then check items on cboDisp,if ItemCount of combobox is 0 then add string "Empty Database" otherwise add list of links. Hope this will help you.


          I Love T-SQL "Don't torture yourself,let the life to do it for you."

          1 Reply Last reply
          0
          • B bapu2889

            hello i am new in sql and i have create one small application with one table which has six columns i want to check recores from sql database but it always shows nothing this is the code bellow in load event, all i want to do is if there is no data in sql database then it should shows "Empty Database" in Combo box but it's not working

            Private Sub LoadCboItems()

                dcIMS = GetDBConnection()
                cboDisp.Items.Clear()
               
                If Me.BindingContext(dsIMS, "IMS").Count = 0 Then   :confused:
                    cboDisp.Text = "Empty Database"
                    Exit Sub
                Else
                    Dim MyReader As SqlDataReader = Nothing
                    Try
                        
                        Dim MyData As New SqlCommand("SELECT \* FROM IMS", dcIMS)
                        MyReader = MyData.ExecuteReader
                        Do While MyReader.Read
                            If rdoID.Checked Then
            
                                cboDisp.Items.Add(MyReader.Item("InkNumber").ToString)
                                cboDisp.SelectedIndex = 0
            
                            ElseIf rdoName.Checked Then
            
                                cboDisp.Items.Add(MyReader.Item("InkName").ToString)
                                cboDisp.SelectedIndex = 0
            
                            End If
                        Loop
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    Finally
                        MyReader.Close()
                        dcIMS.Close()
                        dcIMS.Dispose()
                    End Try
                End If
            
            End Sub
            
            N Offline
            N Offline
            NeverHeardOfMe
            wrote on last edited by
            #5

            Wel, given that by line 3 all you have done is establish a database connection, what can you epect your If... statement to return? Try something more along these lines

            Dim MyData As New SqlCommand("SELECT * FROM IMS", dcIMS
            MyReader = MyData.ExecuteReader
            If MyReader.HasRows Then
            Do While MyReader.Read
            ' ...whatever
            Loop
            Else
            cboDisp.Text = "Empty Database"
            End If
            MyReader.Close()
            dcIMS.Close()

            As an aside, you should not, as a rule, leave Closing your database connection to the Finally clause of a Try block - what happens if an error occurs outside that? Instead, open your Db connections at the last moment possible, and close them immediately after, and test for an open connection in Catch part - eg:

            Dim dbConn As New SqlConnection
            ...
            Try
            ..some code
            dbConn.Open
            .. data amnipulation
            dbConn.Close
            ..more code
            Catch
            If dbConn.State = ConnectionState.Open Then dbConn.Close()
            ...
            End Try
            ...

            B 1 Reply Last reply
            0
            • N NeverHeardOfMe

              Wel, given that by line 3 all you have done is establish a database connection, what can you epect your If... statement to return? Try something more along these lines

              Dim MyData As New SqlCommand("SELECT * FROM IMS", dcIMS
              MyReader = MyData.ExecuteReader
              If MyReader.HasRows Then
              Do While MyReader.Read
              ' ...whatever
              Loop
              Else
              cboDisp.Text = "Empty Database"
              End If
              MyReader.Close()
              dcIMS.Close()

              As an aside, you should not, as a rule, leave Closing your database connection to the Finally clause of a Try block - what happens if an error occurs outside that? Instead, open your Db connections at the last moment possible, and close them immediately after, and test for an open connection in Catch part - eg:

              Dim dbConn As New SqlConnection
              ...
              Try
              ..some code
              dbConn.Open
              .. data amnipulation
              dbConn.Close
              ..more code
              Catch
              If dbConn.State = ConnectionState.Open Then dbConn.Close()
              ...
              End Try
              ...

              B Offline
              B Offline
              bapu2889
              wrote on last edited by
              #6

              Hi Phil first of all thank you very much for your mail and rep. yes it works fine and now it's shows "empty database" if database is empty else it shows inkID so great i didn't know about the has rows but now i learn thanks again :) have a nice day :rose:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups