Results 1 to 23 of 23

Thread: Attemping to pull in lines from a DT

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Attemping to pull in lines from a DT

    I am trying to populate 2 fields on a VB form based on a return of the SQL. The SQL brings back exactly what I want.
    I cannot get passed the 1st state before I get a Index out of bounds. I normally do not use the DT or DS calls but I wanted to ensure the SQL was returning exactly what I want.
    I am certain it is in the READ section with the .getValue but I truly have no clue where I am going wrong. Anyone can give me a quick look-see and advise my error please?

    Data table returns the following ((a snippet):
    Code:
    STATE	TOTAL_MILES	TOTAL_GALLONS
    ALABAMA	          0.00	0.00
    ALASKA	          0.00	0.00
    ARIZONA	          0.00	0.00
    ARKANSAS	       221.50	0.00
    CALIFORNIA	  0.00	0.00
    COLORADO	          0.00	0.00
    CONNECTICUT	  0.00	0.00
    DELAWARE	          0.00	0.00
    DISTRICT OF COLUMBIA	0.00	0.00
    FLORIDA	       267.10	0.00
    GEORGIA	     1769.10	109.01
    HAWAII	          0.00	0.00
    IDAHO	       147.80	0.00
    ILLINOIS	     1224.40	0.00

    I have the following code:

    Code:
        Private Sub SearchByDate()
            Dim dt As New DataTable
            Dim ds As New DataSet()
    
            Dim cmd As New SqlCommand
            Dim reader As SqlDataReader
    
            'SQL to get Mileage & Gallons by State from the State_Miles Table
            cmd.Parameters.Clear()
            cmd.CommandText = "DECLARE @start_month tinyint
                               DECLARE @start_year smallint
                               DECLARE @end_month tinyint
                               DECLARE @end_year smallint
         
                               SET @start_year = '" + cbStartYear.Text + "'
                               SET @start_month = '" + cbStartMonth.Text + "'
                               SET @end_year = '" + cbEndYear.Text + "'
                               SET @end_month = '" + cbEndMonth.Text + "'
    
                               SELECT STATE, ISNULL(SUM(MILES),0) AS TOTAL_MILES, ISNULL(SUM(GALLONS),0) AS TOTAL_GALLONS
                                 FROM State_Miles
                                WHERE STATE LIKE '[A-Z]%'
                                  AND year * 100 + month BETWEEN @start_year * 100 + @start_month 
                                  AND @end_year * 100 + @end_month
                                Group by STATE"
    
            cmd.CommandType = CommandType.Text
            cmd.Connection = New SqlConnection With {
                .ConnectionString = My.MySettings.Default.PETS_DatabaseConnectionString
            }
            cmd.Connection.Open()
            Dim myAdapter As New SqlDataAdapter(cmd)
            myAdapter.Fill(dt)
    
            Try
                reader = cmd.ExecuteReader()
                With reader
                    .Read()
                    ' Writes Data from State Miles table to appropriate Textbox 
                    tbALMiles.Text = .GetValue(1).ToString
                    tbALGals.Text = .GetValue(2).ToString
                    tbAKMiles.Text = .GetValue(4).ToString
                    tbAKGals.Text = .GetValue(5).ToString
                    tbAZMiles.Text = .GetValue(7).ToString
                    tbAZGals.Text = .GetValue(8).ToString
                    tbARMiles.Text = .GetValue(10).ToString
                    tbARGals.Text = .GetValue(11).ToString
                    tbCAMiles.Text = .GetValue(13).ToString
                    tbCAGals.Text = .GetValue(14).ToString
                    tbCOMiles.Text = .GetValue(16).ToString
                    tbCOGals.Text = .GetValue(17).ToString
                    tbCTMiles.Text = .GetValue(19).ToString
                    tbCTGals.Text = .GetValue(20).ToString
                    tbDCMiles.Text = .GetValue(22).ToString
                    tbDCGals.Text = .GetValue(23).ToString
                    tbDEMiles.Text = .GetValue(25).ToString
                    tbDEGals.Text = .GetValue(26).ToString
                    tbDCMiles.Text = .GetValue(28).ToString
                    tbDCGals.Text = .GetValue(29).ToString
                    tbFLMiles.Text = .GetValue(31).ToString
                    tbFLGals.Text = .GetValue(32).ToString
                    tbGAMiles.Text = .GetValue(34).ToString
                    tbGAGals.Text = .GetValue(35).ToString
                    tbIDMiles.Text = .GetValue(37).ToString
                    tbIDGals.Text = .GetValue(38).ToString
                    tbILMiles.Text = .GetValue(40).ToString
                    tbILGals.Text = .GetValue(41).ToString
                    tbINMiles.Text = .GetValue(43).ToString
                    tbINGals.Text = .GetValue(44).ToString
                    tbIAMiles.Text = .GetValue(46).ToString
                    tbIAGals.Text = .GetValue(47).ToString
                    tbKSMiles.Text = .GetValue(49).ToString
                    tbKSGals.Text = .GetValue(50).ToString
                    tbKYMiles.Text = .GetValue(52).ToString
                    tbKYGals.Text = .GetValue(53).ToString
                    tbLAMiles.Text = .GetValue(55).ToString
                    tbLAGals.Text = .GetValue(56).ToString
                    tbMEMiles.Text = .GetValue(58).ToString
                    tbMEGals.Text = .GetValue(59).ToString
                    tbMDMiles.Text = .GetValue(61).ToString
                    tbMDGals.Text = .GetValue(62).ToString
                    tbMAMiles.Text = .GetValue(64).ToString
                    tbMAGals.Text = .GetValue(65).ToString
                    tbMIMiles.Text = .GetValue(67).ToString
                    tbMIGals.Text = .GetValue(68).ToString
                    tbMNMiles.Text = .GetValue(71).ToString
                    tbMNGals.Text = .GetValue(72).ToString
                    tbMSMiles.Text = .GetValue(71).ToString
                    tbMSGals.Text = .GetValue(72).ToString
                    tbMOMiles.Text = .GetValue(74).ToString
                    tbMOGals.Text = .GetValue(75).ToString
                    tbMTMiles.Text = .GetValue(77).ToString
                    tbMTGals.Text = .GetValue(78).ToString
                    tbNEMiles.Text = .GetValue(80).ToString
                    tbNEGals.Text = .GetValue(81).ToString
                    tbNVMiles.Text = .GetValue(83).ToString
                    tbNVGals.Text = .GetValue(84).ToString
                    tbNHMiles.Text = .GetValue(86).ToString
                    tbNHGals.Text = .GetValue(86).ToString
                    tbNJMiles.Text = .GetValue(89).ToString
                    tbNJGals.Text = .GetValue(90).ToString
                    tbNMMiles.Text = .GetValue(92).ToString
                    tbNMGals.Text = .GetValue(93).ToString
                    tbNYMiles.Text = .GetValue(95).ToString
                    tbNYGals.Text = .GetValue(96).ToString
                    tbNCMiles.Text = .GetValue(98).ToString
                    tbNCGals.Text = .GetValue(99).ToString
                    tbNDMiles.Text = .GetValue(101).ToString
                    tbNDGals.Text = .GetValue(102).ToString
                    tbOHMiles.Text = .GetValue(104).ToString
                    tbOHGals.Text = .GetValue(105).ToString
                    tbOKMiles.Text = .GetValue(107).ToString
                    tbOKGals.Text = .GetValue(108).ToString
                    tbORMiles.Text = .GetValue(110).ToString
                    tbORGals.Text = .GetValue(111).ToString
                    tbPAMiles.Text = .GetValue(113).ToString
                    tbPAGals.Text = .GetValue(114).ToString
                    tbRIMiles.Text = .GetValue(116).ToString
                    tbRIGals.Text = .GetValue(117).ToString
                    tbSCMiles.Text = .GetValue(119).ToString
                    tbSCGals.Text = .GetValue(120).ToString
                    tbSDMiles.Text = .GetValue(122).ToString
                    tbSDGals.Text = .GetValue(123).ToString
                    tbTNMiles.Text = .GetValue(125).ToString
                    tbTNGals.Text = .GetValue(126).ToString
                    tbTXMiles.Text = .GetValue(128).ToString
                    tbTXGals.Text = .GetValue(129).ToString
                    tbUTMiles.Text = .GetValue(131).ToString
                    tbUTGals.Text = .GetValue(132).ToString
                    tbVTMiles.Text = .GetValue(134).ToString
                    tbVTGals.Text = .GetValue(135).ToString
                    tbVAMiles.Text = .GetValue(137).ToString
                    tbVAGals.Text = .GetValue(138).ToString
                    tbWAMiles.Text = .GetValue(140).ToString
                    tbWAGals.Text = .GetValue(141).ToString
                    tbWVMiles.Text = .GetValue(143).ToString
                    tbWVGals.Text = .GetValue(144).ToString
                    tbWIMiles.Text = .GetValue(146).ToString
                    tbWIGals.Text = .GetValue(147).ToString
                    tbWYMiles.Text = .GetValue(149).ToString
                    tbWYGals.Text = .GetValue(150).ToString
    
                    Try ' Writes Data from State Miles table to appropriate Textbox 
                    Catch ex As Exception
                        Return
                    End Try
    
                End With
                reader.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            cmd.Connection.Close()
            MPGTotals()
        End Sub
    Last edited by K3JAE; Jan 31st, 2021 at 10:11 PM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Attemping to pull in lines from a DT

    Your select statement is returning 3 columns of data, so .GetValue(n) is only valid when n = 0, 1, or 2. Anything greater than 2 is going to be an index out of bounds.

    Beyond that... Well, not sure where to start.

    Good luck.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Attemping to pull in lines from a DT

    Your query pulls back three columns, which means that your data reader only has three valid indexes: 0, 1 and 2. Your result set contains multiple records so, to read all the data, you need to call Read multiple times, once for each record. Usually, you would read the data in a loop:
    vb.net Code:
    1. While myDataReader.Read()
    2.     Dim firstField = myDataReader.GetString(0)
    3.     Dim secondField = myDataReader.GetString(1)
    4.     'Etc.
    5. End While
    Read will return True each time it reads a record and False when it has reached the end of the data.

    Your problem is that you need to use different TextBoxes each time, which means you can just read each record and do the same thing each time:
    vb.net Code:
    1. myDataReader.Read()
    2. tbALMiles.Text = myDataReader.GetInt32(1).ToString()
    3. tbALGals.Text = myDataReader.GetInt32(2).ToString()
    4. myDataReader.Read()
    5. tbAKMiles.Text = myDataReader.GetInt32(1).ToString()
    6. tbAKGals.Text = myDataReader.GetInt32(2).ToString()
    7. 'Etc.
    One option might be to put all the TextBoxes in a list and loop over that:
    vb.net Code:
    1. Dim textBoxPairs = {Tuple.Create(tbALMiles, tbALGals}, Tuple.Create(tbAKMiles, tbAKGals}, 'Etc.}
    2.  
    3. For Each textBoxPair In textBoxPairs
    4.     myDataReader.Read()
    5.     textBoxPair.Item1.Text = myDataReader.GetInt32(1).ToString()
    6.     textBoxPair.Item2.Text = myDataReader.GetInt32(2).ToString()
    7. Next
    That assumes that the number of records matches the number of TextBox pairs and that they are in the corresponding order.

    Another option would be to modify your query such that the result set contained the same state code as the names of the Textboxes and then use that to find the correct TextBoxes, e.g.
    vb.net Code:
    1. While myDataReader.Read()
    2.     Dim stateCode = myDataReader.GetString(0)
    3.  
    4.     Controls($"tb{stateCode}Miles").Text = myDataReader.GetInt32(1).ToString()
    5.     Controls($"tb{stateCode}Gals").Text = myDataReader.GetInt32(2).ToString()
    6.     'Etc.
    7. End While

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    OK, I had originally created a SQL for each state. This worked fine, but was lengthy code requiring multiple open/closing of SQL connections, as you can imagine. What I was hoping to do is get the entire dataset in one connection then pull the data off each row.

    The most viable options you give, in my opinion, was your 2nd or 3rd options.

    Problem, the data set only has full name states. No abbreviations. Where I need a State abbreviation I join to another table containing abbreviations.
    I could potentially add the abbreviations in the table I'm pulling from to resolve that particular issue, but then we are duplicating data. So I am not sure if there is an efficient way to do this without opening and closing connections for each SQL query.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Attemping to pull in lines from a DT

    You can just add a Join to the SQL statement, and return the appropriate field, eg:
    Code:
                               SELECT STATE, ISNULL(SUM(MILES),0) AS TOTAL_MILES, ISNULL(SUM(GALLONS),0) AS TOTAL_GALLONS
                                         , States.STATE_ABBREVIATION
                                 FROM State_Miles
                                 INNER JOIN States ON State_Miles.STATE = States.STATE_NAME
                                WHERE STATE LIKE '[A-Z]%'
                                ...
    (I've guessed at the table/field names)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT - RESOLVED

    I have resolved my issue by utilizing jmcilhinney's 3rd option. Thank you sir. I literally just deleted near 1000 lines of code by utilizing that method which works flawlessly. Much appreciated.

    I decided, for the sake of easing the pain a little to add in a State Abbreviation column to the table to simplify the transition to the new code. Suffice it to say, it works beautifully and accurately. VERY much appreciate the assist... I knew it was something to do with the reader area of the code, just was unsure how to implement it so avoid the lengthy code I had which now takes up less resources and appears to be a little quicker getting the data to the form.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT - RESOLVED

    I do have another question:

    Is there a quick and dirtly way to do the following as opposed to coding in every state text field?

    Code:
        Private Sub StateColor()
            tbALMiles.ForeColor = If(CDec(tbALMiles.Text) = CDec("0"), Color.Gray, Color.Black)
            tbAKMiles.ForeColor = If(CDec(tbAKMiles.Text) = CDec("0"), Color.Gray, Color.Black)
            tbAZMiles.ForeColor = If(CDec(tbAZMiles.Text) = CDec("0"), Color.Gray, Color.Black)
            tbARMiles.ForeColor = If(CDec(tbARMiles.Text) = CDec("0"), Color.Gray, Color.Black)
            tbCAMiles.ForeColor = If(CDec(tbCAMiles.Text) = CDec("0"), Color.Gray, Color.Black)
    		
    	tbALGals.ForeColor = If(CDec(tbALGals.Text) = CDec("0"), Color.Gray, Color.Black)
            tbAKGals.ForeColor = If(CDec(tbAKGals.Text) = CDec("0"), Color.Gray, Color.Black)
            tbAZGals.ForeColor = If(CDec(tbAZGals.Text) = CDec("0"), Color.Gray, Color.Black)
            tbARGals.ForeColor = If(CDec(tbARGals.Text) = CDec("0"), Color.Gray, Color.Black)
            tbCAGals.ForeColor = If(CDec(tbCAGals.Text) = CDec("0"), Color.Gray, Color.Black)
       End Sub

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Attemping to pull in lines from a DT

    You can simplify it using an array and a loop, eg:
    Code:
        Private Sub StateColor()
            Dim controlsToColor() as Textbox = {tbALMiles, tbAKMiles, tbAZMiles, 
                                                tbARMiles.Text, ... }
    
            For each C in controlsToColor
               c.ForeColor = If(CDec(c.Text) = CDec("0"), Color.Gray, Color.Black)
            Next
       End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by si_the_geek View Post
    You can simplify it using an array and a loop, eg:
    Code:
        Private Sub StateColor()
            Dim controlsToColor() as Textbox = {tbALMiles, tbAKMiles, tbAZMiles, 
                                                tbARMiles.Text, ... }
    
            For each C in controlsToColor
               c.ForeColor = If(CDec(c.Text) = CDec("0"), Color.Gray, Color.Black)
            Next
       End Sub
    That is perfect. I forgot about doing it that way... DOH!

    I did make a modification somewhat so my form did not crash on a NULL return for a state... Wondering if this is a solid way to get around formatting the textboxes that have a null return. It does appear to work fine with no apparent notifications in debug. I have as follows:

    Code:
    Private Sub StateColor()
            Dim controlsToColor() as Textbox = {tbALMiles, tbAKMiles, tbAZMiles, tbARMiles, tbCAMiles, tbCOMiles, tbCTMiles, tbDEMiles, tbDCMiles, tbFLMiles, tbGAMiles, tbHIMiles, tbIDMiles, 
                                                tbILMiles, tbINMiles, tbIAMiles, tbKSMiles, tbKYMiles, tbLAMiles, tbMEMiles, tbMDMiles, tbMAMiles, tbMIMiles, tbMNMiles, tbMSMiles, tbMOMiles, 
                                                tbMTMiles, tbNEMiles, tbNVMiles, tbNHMiles, tbNJMiles, tbNMMiles, tbNYMiles, tbNCMiles, tbNDMiles, tbOHMiles, tbOKMiles, tbORMiles, tbPAMiles, 
                                                tbRIMiles, tbSCMiles, tbSDMiles, tbTNMiles, tbTXMiles, tbUTMiles, tbVTMiles, tbVAMiles, tbWAMiles, tbWVMiles, tbWIMiles, tbWYMiles,
                                                tbALGals, tbAKGals, tbAZGals, tbARGals, tbCAGals, tbCOGals, tbCTGals, tbDEGals, tbDCGals, tbFLGals, tbGAGals, tbHIGals, tbIDGals, tbILGals, 
                                                tbINGals, tbIAGals, tbKSGals, tbKYGals, tbLAGals, tbMEGals, tbMDGals, tbMAGals, tbMIGals, tbMNGals, tbMSGals, tbMOGals, tbMTGals, tbNEGals, 
                                                tbNVGals, tbNHGals, tbNJGals, tbNMGals, tbNYGals, tbNCGals, tbNDGals, tbOHGals, tbOKGals, tbORGals, tbPAGals, tbRIGals, tbSCGals, tbSDGals, 
                                                tbTNGals, tbTXGals, tbUTGals, tbVTGals, tbVAGals, tbWAGals, tbWVGals, tbWIGals, tbWYGals}
    
            For Each C In controlsToColor
                Dim A As Decimal = If(C.Text.Trim IsNot "", CDec(C.Text.Trim), 0D)
                C.ForeColor = If(A = 0D, Color.Gray, Color.Black)
            Next
        End Sub
    Last edited by K3JAE; Feb 2nd, 2021 at 05:33 PM.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Attemping to pull in lines from a DT

    There is no reason to create a decimal and treat it as a boolean. Just use a boolean:
    Code:
    For Each c In controlsToColor
        c.ForeColor = If(String.IsNullOrWhitespace(c.Text.Trim()), Color.Gray, Color.Black)
    Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by dday9 View Post
    There is no reason to create a decimal and treat it as a boolean. Just use a boolean:
    Code:
    For Each c In controlsToColor
        c.ForeColor = If(String.IsNullOrWhitespace(c.Text.Trim()), Color.Gray, Color.Black)
    Next
    The above handles the issue of a null return but NOT the issue of a "0" return. The intended goal is to color a returned "0" to gray. There is no reason to color a null return which is what your code is handling.

    Either function is what I am intending on doing... when I search on a specific state one of two things would happen:
    1. All the other 'non-searched' states miles and gallons populate with "0" in the miles and gallons textboxes, (not critical) if a null is returned. Currently those states return a null value and those fields do not populate, which is fine.
    2. If a specific state is searched and either miles or gallons or both return a '0' then the "0" fore color is changed to gray otherwise if it is >0 then the fore color should be black
    Last edited by K3JAE; Feb 2nd, 2021 at 04:16 PM.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Attemping to pull in lines from a DT

    If that is the case, then try this:
    Code:
    For Each c In controlsToColor
        Dim value As Decimal
        If (Decimal.TryParse(c.Text.Trim(), value) AndAlso value = 0) Then
            c.ForeColor = Color.Gray
        Else
            c.ForeColor = Color.Black
        End If
    Next
    This will set the ForeColor to Gray if the text can be converted to a Decimal and the Decimal value is 0. Otherwise, if neither of those two conditions are true, then set the ForeColor to Black.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Your code results in the same thing I initially submitted above that appears to work the same. Please understand, I am not being argumentative, just trying to understand what functionally is different in your code than what I came up with.

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Attemping to pull in lines from a DT

    I think if you change "AndAlso value = 0" to "AndAlso value <> 0" you will get what you want.

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by K3JAE View Post
    Your code results in the same thing I initially submitted above that appears to work the same. Please understand, I am not being argumentative, just trying to understand what functionally is different in your code than what I came up with.
    If the code does what you want it to do, here are the reason why what I provided is better/different:
    1. My code will only touch the String once. Strings are immutable in .NET and so you want to touch them as little as possible.
    2. My code evaluates the value of the converted decimal whereas yours compares the reference of the String. IsNot compares references whereas <> compares values. In this particular case, it probably doesn't matter so much, but the basic principle is that you are wanting to compare the values and not the references. For more information: https://jonskeet.uk/csharp/references.html
    3. (this one is subjective) My code uses the .NET method TryParse whereas yours uses the Visual Basic method CDec. I am a firm believer that if you are writing Visual Basic .NET code, then write using .NET methods and not legacy syntax holdovers. In my opinion, this will also help people who are reading your code in that they do not have to lookup what the code is doing. They can literally read: "ok, this code is trying to parse a decimal value" whereas 10 years from now, they may say "what in the heck is CDec? Is it currency? Dec is definitely an abbreviation for decimal, I think."


    If the code does not do what you want it to do, then you'll need to adjust the logic. But re-reading post #11, I think that I have the logic right.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by dday9 View Post
    My code uses the .NET method TryParse whereas yours uses the Visual Basic method CDec. I am a firm believer that if you are writing Visual Basic .NET code, then write using .NET methods and not legacy syntax holdovers. In my opinion, this will also help people who are reading your code in that they do not have to lookup what the code is doing. They can literally read: "ok, this code is trying to parse a decimal value" whereas 10 years from now, they may say "what in the heck is CDec? Is it currency? Dec is definitely an abbreviation for decimal, I think."
    Personally, I only use CDec, CStr, etc, for casting, i.e. get a reference/value of the appropriate type to an object that is already that type, e.g.
    vb.net Code:
    1. Dim obj = 123D
    2. Dim dec = CDec(obj)
    If you want to actually convert a String to a Decimal then use Convert.ToDecimal, Decimal.Parse or Decimal.TryParse. TryParse is the obvious best choice if the data needs validation, but the other options are better if you already know for sure that the data will be valid.

  17. #17
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Attemping to pull in lines from a DT

    2. If a specific state is searched and either miles or gallons or both return a '0' then the "0" fore color is changed to gray otherwise if it is >0 then the fore color should be black
    After rereading post #11, now I'm confused. It may be the OP is talking about two separate textboxes, miles and gallons. If either one (or both) is not a decimal or =0 then the forecolor should be gray for BOTH.

    Maybe I'm reading this wrong,

    Code:
        If (Decimal.TryParse(c.Text.Trim(), value) AndAlso value = 0) Then
            c.ForeColor = Color.Gray
    Doesn't this say, If the textbox text property IS a Decimal AND it = 0 then set the ForeColor to Gray.

    This would only be true if the textbox text equals 0
    Last edited by wes4dbt; Feb 2nd, 2021 at 10:56 PM.

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by wes4dbt View Post
    Doesn't this say, If the textbox text property IS a Decimal AND it = 0 then set the ForeColor to Gray.
    That was my understanding.

    Quote Originally Posted by wes4dbt View Post
    After rereading post #11, now I'm confused. It may be the OP is talking about two separate textboxes, miles and gallons. If either one (or both) is not a decimal or =0 then the forecolor should be gray for BOTH.
    After this statement and rereading #11, I'm not as confident in my understanding of what the OP wants to do.

    @K3JAE - If the business logic is incorrect, could you provide a bit more clarification for me?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by dday9 View Post
    That was my understanding.
    After this statement and rereading #11, I'm not as confident in my understanding of what the OP wants to do.

    @K3JAE - If the business logic is incorrect, could you provide a bit more clarification for me?
    Let me clarify my request:

    I am look to populate 2 pieces of data : Miles and Gallons into 2 separate textboxes for each state.

    First step:
    If the data returned is NULL convert to a '0'.

    Second step:
    Once the data is returned, the NULL has been converted to zero if appropriate, look at the end results.
    If either Miles or Gallons or both returns a '0' then the '0' fore color is changed to gray for that specific field otherwise if it is >0 then the fore color should be black.

    Assume ARKANSAS (AR) returns 1500 miles and 0 gallons.
    The Miles (1500) then would be black. The Gallons (0) would be colored gray.

    I also would like all other states not in the search criteria to complete with '0' in both the MILES/Gallons textbox. This however is NOT a requirement or an absolute necessity if it cannot be done easily.
    Last edited by K3JAE; Feb 3rd, 2021 at 03:28 AM.

  20. #20
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Attemping to pull in lines from a DT

    Once the data is returned, the NULL has been converted to zero if appropriate, look at the end result and if the result is '0' then turn it gray, - if the result is >0 then the fore color should be black.
    Just to be clear. What would you want if the Miles textbox contains 0 and the Gallons textbox contains 10. Miles textbox forecolor is gray and the Gallons textbox forecolore is Black. If so,

    Code:
    For Each c In controlsToColor
        Dim value As Decimal
        If (Decimal.TryParse(c.Text.Trim(), value) AndAlso value <> 0) Then
            c.ForeColor = Color.Black
        Else
            c.ForeColor = Color.Gray
        End If
    Next
    Oh, you edited your post while I was posting.

    This is clarification I was looking for.

    Assume ARKANSAS (AR) returns 1500 miles and 0 gallons.
    The Miles (1500) then would be black. The Gallons (0) would be colored gray.
    Last edited by wes4dbt; Feb 3rd, 2021 at 01:14 AM.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Quote Originally Posted by wes4dbt View Post
    Just to be clear. What would you want if the Miles textbox contains 0 and the Gallons textbox contains 10. Miles textbox forecolor is gray and the Gallons textbox forecolore is Black. If so,

    Code:
    For Each c In controlsToColor
        Dim value As Decimal
        If (Decimal.TryParse(c.Text.Trim(), value) AndAlso value <> 0) Then
            c.ForeColor = Color.Black
        Else
            c.ForeColor = Color.Gray
        End If
    Next
    Oh, you edited your post while I was posting.

    This is clarification I was looking for.

    Just as a tid-bit add-on of a clarification... MILES can never be '0' if there is ANY gallons purchased. To put it simply one cannot by fuel in a state if one has not entered into said state.

  22. #22
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Attemping to pull in lines from a DT

    If your question of modifying the forecolore has been resolved you should mark this thread as resolved. You should start a new thread with the appropriate title if your having trouble solving an additional issue.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    97

    Re: Attemping to pull in lines from a DT

    Good idea.... Thanks for those who assisted and it is much appreciated. I have learned a LOT and will utilize that new knowledge in my many other forms where required.

    Hats off to ALL for the assist.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width