Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, the code looks right to me but the selected item is not writing to the database. If I change my if to "Not selected" both of the values write fine.
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As New DataTable
        Dim folder As String = "\\Server\folder$\" & Request("UR")
        'Dim folder As String = "\\Server\folder$\" & ("UR")
        dt.Columns.Add(New DataColumn("FileName", GetType(String)))
        dt.Columns.Add(New DataColumn("cbsel", GetType(Boolean)))
        dt.Columns.Add(New DataColumn("file", GetType(String)))
        For Each sFilePath As String In Directory.GetFiles(folder)
            Dim sFileName As String = Path.GetFileName(sFilePath)
            Dim row As DataRow = dt.NewRow
            row("FileName") = "<a href=""" & Server.HtmlEncode("file://" & Replace(sFilePath, "\", "/")) & """ target=""_new"">" & Server.HtmlEncode(sFileName) & "</a>"
            ' row("cbsel") = True
            row("file") = folder & "\" & sFileName

            dt.Rows.Add(row)
        Next

        GridView1.DataSource = New DataView(dt)
        GridView1.DataBind()
    End Sub


    Protected Sub selectimg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles selectimg.Click

        Dim cellCode As TableCell
        Dim row As GridViewRow
        Try
            For Each row In GridView1.Rows
                Dim cellSelect As CheckBox = row.FindControl("cbsel")
                If cellSelect.Checked Then
                    cellCode = row.Cells(0)
                    Dim FileSelected As String = cellCode.Text

                    Dim con As New SqlConnection
                    Dim cmd As SqlCommand
                    con.ConnectionString = "Data Source=server;Initial Catalog=database;Persist Security Info=True;User ID=username;Password=password"
                    con.Open()
                    cmd = con.CreateCommand()
                    cmd.Connection = con
                    cmd.CommandText = "INSERT INTO Test_table (test) VALUES (@Test)"
                    cmd.Parameters.AddWithValue("@Test", FileSelected)
                    cmd.ExecuteNonQuery()


                    cellSelect.Checked = False
                End If
            Next row
        Finally
            GridView1.DataBind()
        End Try

    End Sub
Posted

your missing the catch in your try catch finally block. I would put that back in see if an error is caught.

Try catch examples[^]
 
Share this answer
 
Okie I have added a message box in. Still no error. It doesn't error because it all works fine. It just passes the check boxes as false .

VB
Private Sub MessageBox(ByVal msg As String)
    Dim lbl As New Label()
    lbl.Text = "<script language='javascript'>" & Environment.NewLine & _
    "window.alert('" + msg + "')</script>"
    Page.Controls.Add(lbl)
End Sub


VB
        End If
    Next row
Catch ex As Exception
    MessageBox("It's broken!")
Finally
    GridView1.DataBind()
End Try
 
Share this answer
 
v2
Comments
aidin Tajadod 15-Feb-12 20:01pm    
you need to put a if (!IsPostBack) on your page_load and write your your inside the if. when ever you change your checkbox, your page_load runs first! so no checkbox is checked!
use .value=false or .value=true Because then system check the value of the boolean column
 
Share this answer
 

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