Hi guys,

Issue is I have a grid view that I need to select certain values from to go into a SQL table. The following is my code from creating the grid though to the select button.

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

What are you trying to do with this code, completely load a file into sql?

No only the file name.

Try this,

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then

        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 If
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.