Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 Sub read()
        Dim constring As String = "dsn=mysql_tabel;database=db1;server=localhost;iud=root"
        Dim con As New OdbcConnection(constring)
        'Dim con As New OdbcCommand(SQL)
        'dataload()
        con.Open()
        Dim query As String = "Select *From tbdata_reservasi"
        Dim cmd As New OdbcCommand(query, con)
        Dim dr As OdbcDataReader = cmd.ExecuteReader()

        While dr.Read
            Dim RoomNum_R As Integer = dr.GetInt32(7)
            Dim startdate As DateTime = dr.GetDate(9)
            Dim enddate As DateTime = dr.GetDate(10)

            Dim startdate_c As Integer = startdate.Day
            Dim enddate_c As Integer = enddate.Day
            Dim room1 As Integer = RoomNum_R - 1 
            Dim totaldays As Integer = enddate_c - startdate_c

            DG1(startdate_c, room1).Style.BackColor = Color.Red

            For i = 1 To totaldays
                DG1(startdate_c + i, room1).Style.BackColor = Color.Red

                totaldays -= 1
                'totaldays = -1

            Next

        End While
    End Sub

What I have tried:

i dont know for solution,if the number of RoomNum only 1 digit there is no error,but if mor digit at the RoomNum it will be error "<pre>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Posted
Updated 22-May-23 1:54am

It looks your DG1 array (whatever it is) has not enough rooms... :-)
I suggest you to use the debugger to gain some insight.
 
Share this answer
 
Comments
Joebpn1 22-May-23 7:41am    
DG1 is gridview1
Joebpn1 22-May-23 7:49am    
can you help for the array?
At a guess, DG1 is a DataGrid of some form, and you are trying to set the background colour for rooms, but boy are there problems with that code ...
Your For loop runs from 1 (which isn't the index of the first row - they are numbered from 0 in .NET) to a value that you decrease each time you go round it ...

Instead of playing with the colours, do it when you draw the control - this may help: Colouring DataGridView Cells According to their Content in WinForms[^] the code is in C#, but onlone converters exist and can help you you don't understand: Code Converter C# to VB and VB to C# – Telerik[^]
 
Share this answer
 
Comments
Joebpn1 22-May-23 9:41am    
the error from RoomNum bro...if the RoomNum only 1 digit there is no error...but if the RoomNum 2 digit like 10 or more it make the code error..(RoomNum is data room number from database)
OriginalGriff 22-May-23 10:09am    
Yes, because you use that as the column index:
Dim room1 As Integer = RoomNum_R - 1 
...
DG1(startdate_c + i, room1).Style.BackColor = Color.Red
So if your DG has less than 10 columns ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900