Click here to Skip to main content
15,887,272 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 GridViews in my Web App.

GridView1 contains the entire date selection for the selected month along with a checkbox column. When ticked the date plus additional info is updated to SQL database. Untick deletes the record.

GridView2 is a hidden GridView which contains data of dates from GridView1 that was previously ticked.

My idea is to have GridView1 compare with GridView2 for their dates and if GridView2 have date that matches the selection in GridView1, it will automatically tick it when GridView1 loads.

What I have tried:

Design for GridView1
ASP.NET
<asp:GridView ID="GridView1" runat="server" BackColor="#FFFFCC" 
            ShowHeaderWhenEmpty="True" Width="100%">         

             <Columns>
                
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="UpdateDB_click"
                       autopostback = "true" Wrap="True" />
                </ItemTemplate>
            </asp:TemplateField>

             </Columns> 

        </asp:GridView>


The GridView2 will be loaded at the same time with GridView1, so they will share the same sub.

VB
Sub load_Calander()

        Dim year As Integer = Convert.ToInt32(CurrentYear.Text)
        Dim month As Integer = Convert.ToInt32(Mth_Num.Text)
        If month = 1 Then

            Mth_Txt.Text = "Jan"

        ElseIf month = 2 Then

            Mth_Txt.Text = "Feb"

        ElseIf month = 3 Then

            Mth_Txt.Text = "Mar"

        ElseIf month = 4 Then

            Mth_Txt.Text = "Apr"

        ElseIf month = 5 Then

            Mth_Txt.Text = "May"

        ElseIf month = 6 Then

            Mth_Txt.Text = "Jun"

        ElseIf month = 7 Then

            Mth_Txt.Text = "Jul"

        ElseIf month = 8 Then

            Mth_Txt.Text = "Aug"

        ElseIf month = 9 Then

            Mth_Txt.Text = "Sep"

        ElseIf month = 10 Then

            Mth_Txt.Text = "Oct"

        ElseIf month = 11 Then

            Mth_Txt.Text = "Nov"

        ElseIf month = 12 Then

            Mth_Txt.Text = "Dec"

        End If

        Dim myList As List(Of [myClass]) = New List(Of [myClass])()

        If month = 1 OrElse month = 3 OrElse month = 5 OrElse month = 7 OrElse month = 8 OrElse month = 10 OrElse month = 12 Then

            For i As Integer = 1 To 31

                Dim item As [myClass] = New [myClass]()
                item.[Date] = String.Format("{0}/{1}/{2}", i.ToString(), Mth_Txt.Text, CurrentYear.Text)
                Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
                item.[Day] = dateValue.ToString("ddddddddd")

                myList.Add(item)

            Next

        ElseIf month = 4 OrElse month = 6 OrElse month = 9 OrElse month = 11 Then

            For i As Integer = 1 To 30

                Dim item As [myClass] = New [myClass]()
                item.[Date] = String.Format("{0}/{1}/{2}", i.ToString(), Mth_Txt.Text, CurrentYear.Text)
                Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
                item.[Day] = dateValue.ToString("ddddddddd")
                myList.Add(item)

            Next

        ElseIf DateTime.IsLeapYear(year) Then

            For i As Integer = 1 To 29

                Dim item As [myClass] = New [myClass]()
                item.[Date] = String.Format("{0}/{1}/{2}", i.ToString(), Mth_Txt.Text, CurrentYear.Text)
                Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
                item.[Day] = dateValue.ToString("ddddddddd")
                myList.Add(item)

            Next

        Else

            For i As Integer = 1 To 28

                Dim item As [myClass] = New [myClass]()
                item.[Date] = String.Format("{0}/{1}/{2}", i.ToString(), Mth_Txt.Text, CurrentYear.Text)
                Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
                item.[Day] = dateValue.ToString("ddddddddd")
                myList.Add(item)

            Next

        End If

        GridView1.DataSource = myList
        GridView1.DataBind()

        'load GridView2
        Dim cmd As New SqlCommand
        Dim DateTB As New DataSet

        Dim DateCmd As String = "select ScheduleDate FROM [SQLIOT].[dbo].[ZEPB_PreventiveDateSchedule] " _
                                & " where line_desc = '" & LineList.SelectedItem.Text & "' and process = '" & ProcessList.SelectedItem.Text & "' " _
                                & " and machine = '" & MachineList.SelectedItem.Text & "' and Step = '" & StepList.SelectedItem.Text & "' " _
                                & " and inspection = '" & InspectionList.SelectedItem.Text & "' and  specification = '" & SpecificationList.SelectedItem.Text & "' "

        Dim da As New SqlDataAdapter(DateCmd, Conn)

        cmd.Connection = Conn

        Conn.Open()
        da.Fill(DateTB, "TempStore")

        Dim dvgroup As DataView = DateTB.Tables("TempStore").DefaultView

        GridView2.DataSource = dvgroup
        GridView2.DataBind()

        Conn.Close()


I want to build an IF-ELSE statement but my knowledge is limited:

VB
Dim rowNo As Integer = (CType((TryCast(sender, Control)).NamingContainer, GridViewRow)).RowIndex 'sender not declared error

Dim CheckBoxChecked As Boolean = CType(sender, CheckBox).Checked 
'To be used to send signal to auto-tick checkbox. sender not declared error

 If GridView1.Rows.... 'cannot find the proper extension, leaving me stuck 
Posted
Comments
Richard Deeming 8-Apr-21 3:58am    
Dim DateCmd As String = "select ScheduleDate FROM [SQLIOT].[dbo].[ZEPB_PreventiveDateSchedule] " _
& " where line_desc = '" & LineList.SelectedItem.Text & "' and process = '" & ProcessList.SelectedItem.Text & "' " _
& " and machine = '" & MachineList.SelectedItem.Text & "' and Step = '" & StepList.SelectedItem.Text & "' " _
& " and inspection = '" & InspectionList.SelectedItem.Text & "' and  specification = '" & SpecificationList.SelectedItem.Text & "'

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

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