Solved

Help with accessing a sql database in VB.NET 2005

Posted on 2006-07-15
4
197 Views
Last Modified: 2010-04-23
I use the following code in my program.

        sQsT = "SELECT * FROM (Payroll) WHERE (FirstName  = '" & fN & "') AND (LastName = '" & lN & "') AND (dEnd >= #" & DateTimePicker1.Value & "#) AND (dEnd <= #" & DateTimePicker2.Value & "#)"

        On Error Resume Next
        'Declare SQL Connection, Data Adapter and Dataset
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")
        Dim da As New System.Data.SqlClient.SqlDataAdapter(sQsT, conn)
        Dim ds As New System.Data.DataSet
        Dim dr As DataRow

        da.Fill(ds)
        'Find out how many Rows are in the First table of the data Adapter
        Dim DCT As Integer = ds.Tables(0).Rows.Count

        For NM As Integer = 0 To DCT - 1
            dr = ds.Tables(0).Rows(NM)

            eiInsurance += dr.Item("HealthInsurance")
            eiTotald += dr.Item("TotalEmployerContributions")
            iGrossSalary += dr.Item("GrossSalary")
            iNetSalary += dr.Item("NetSalary")

        Next

although the data is in the database, it is not going into the loop to load the variables. I am not sure what I did wrong and any advice or assistance would be greatly appreciated.
0
Comment
Question by:OsirisJa
  • 2
4 Comments
 
LVL 6

Assisted Solution

by:Nandakumar Sakthivel
Nandakumar Sakthivel earned 150 total points
ID: 17113891
I think that the problem is in your select command.Everything looks fine except that
Can u check the select command(In query analyser) at runtime after the values has been replaced

Thanks,
Nanda
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 150 total points
ID: 17113934
Is # the right marker for dates in SQL Express?  It is in Access, but I think that's specific to Access.

I suggest you try it without that, or with ' instead (perhaps with DateTimePicker2.Value.ToString).

Roger
0
 
LVL 13

Expert Comment

by:iHadi
ID: 17114093
Hi OsirisJa
There could be more than one reasons for your problem
-  use ' instead of # as Sancler suggested
-  Check the date format you are using: Try to format the date the same format as SQL Server so the server doesn't consider the month as day and the day as month
-  Your select statment is selecting a date with the time zero eg: 2005-05-08 00:00:00.000 so if the time in the stored datetime in the database is not zero eg: 2005-05-08 14:58:00.000 then the select statment will ignore it
    To correct this use the select statement as follows:
    SELECT * FROM (Payroll)
    WHERE dEnd >= '2005-05-08 '
    AND dEnd <'2005-05-09 '
0
 
LVL 13

Accepted Solution

by:
iHadi earned 200 total points
ID: 17114098
Hi OsirisJa
There could be more than one reasons for your problem
-  use ' instead of # as Sancler suggested
-  Check the date format you are using: Try to format the date the same format as SQL Server so the server doesn't consider the month as day and the day as month
-  Your select statment is selecting a date with the time zero eg: 2005-05-08 00:00:00.000 so if the time in the stored datetime in the database is not zero eg: 2005-05-08 14:58:00.000 then the select statment will ignore it
    To correct this use the select statement as follows:
    SELECT * FROM (Payroll)
    WHERE dEnd >= '2005-05-08'
    AND dEnd <'2005-05-09'
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - Get ColumnNames of SQL Table 4 15
VB.net Progress Bar - Maximum Value too large 2 15
Generate Numbers in JQuery file 11 66
"Emulate" TAB key when press Enter Key 3 47
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question