Only one value gets inserted from the selected checkboxes

Discussion in 'C#' started by ravisakee, Dec 16, 2008.

  1. #1
    Hi , i am new to asp and i recently migrated to asp from asp.net for changes to a application developed using asp.
    My requirement is i am displaying values from database in a html table ,the user selects the some of the rows by using checkbook.
    on click of button the selected check box value should get inserted into database.I am able to insert the values but only one value gets inserted.
    i am splitting the the checkbox value and storing it in word array..
    then inserting it one by one...
    when i print the values one by one its displaying all.
    here goes my code

    <% @language = "VBScript" %>
    <html>
    <head>
    </head>
    <form action="get">
    <body>
    <%
    Dim conno
    conno=request.form("some_name")
    Dim ConnString
    ConnString = "Driver=SQL Server;server=sakee;uid=sa;pwd=admin;initial catalog=iguard"
    set conn=server.createobject("adodb.connection")
    conn.Open ConnString
    Dim flag,i,cono1
    flag = false
    if(inStr(conno,","))then
    cono1=Split(conno,",")
    flag = true
    end if
    if(flag = false)then
    Set rs=conn.execute("INSERT into tblContainers (Container_ID,Container_No,Seal_No,Carrier,Entered _By,Entered_Date,Origin,Arrival_Date,Arrival_Time) SELECT CONVERT(VARCHAR(8), GETDATE(), 112) + '-' + SUBSTRING(com.container_no, 1, 11) + '-' + SUBSTRING(CAST(NEWID() AS VARCHAR(40)), 1, 3) AS Container_ID,SUBSTRING(com.container_no, 1, 11) AS Container_No,com.Seal_No,com.Carrier,'IGuardTracki ng' AS Entered_By,GETDATE() AS Entered_Date,cotr.Origin,(cast(CONVERT(datetime, com.Arrival_Date, 101) AS varchar(20))) aS Arrival_Date,CONVERT(VARCHAR(20), com.Arrival_Date,109) AS Arrival_Time FROM DehartGroup.dbo.Container_Master AS com INNER JOIN DehartGroup.dbo.Container_Tracing AS cotr ON (cotr.Container_No = com.Container_No) AND (cotr.Seal_No = com.Seal_No) AND (com.Container_No='"+conno+"') ")
    end if
    if(flag = true) then
    for i=0 to uBound(cono1)
    Set rs=conn.execute("INSERT into tblContainers (Container_ID,Container_No,Seal_No,Carrier,Entered _By,Entered_Date,Origin,Arrival_Date,Arrival_Time) SELECT CONVERT(VARCHAR(8), GETDATE(), 112) + '-' + SUBSTRING(com.container_no, 1, 11) + '-' + SUBSTRING(CAST(NEWID() AS VARCHAR(40)), 1, 3) AS Container_ID,SUBSTRING(com.container_no, 1, 11) AS Container_No,com.Seal_No,com.Carrier,'IGuardTracki ng' AS Entered_By,GETDATE() AS Entered_Date,cotr.Origin,(cast(CONVERT(datetime, com.Arrival_Date, 101) AS varchar(20))) aS Arrival_Date,CONVERT(VARCHAR(20), com.Arrival_Date,109) AS Arrival_Time FROM DehartGroup.dbo.Container_Master AS com INNER JOIN DehartGroup.dbo.Container_Tracing AS cotr ON (cotr.Container_No =com.Container_No) AND (cotr.Seal_No = com.Seal_No) AND com.Container_No in ('"+cono1(i)+"') ")
    response.write(cono1(i))

    Next
    end if
    set conn =Nothing
    set rs = nothing
    %>
    <p>Data Transferd successfully</p>


    </form>
    </body>
    </html>

    The underlined statement is print all the selected values one by one .
    i am getting checkbox values from prevous page

    Please do help
    Thanks
    RAvi
     
    ravisakee, Dec 16, 2008 IP
  2. islandhopper8

    islandhopper8 Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    73
    #2
    Hi,

    I think you are looking for the following (I am using Mysql, so you just have to make some small changes)
    On the page you have to create a panel (panel1) in which the checkboxes are created.
    In the page load the checkboxes are being set up as follows:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim myConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
    Dim sqlString As String = Nothing
    Dim myConnection As New MySqlConnection(myConnectionString)
    Dim myAdapter As MySqlDataAdapter = New MySqlDataAdapter(sqlString, myConnection)
    myAdapter.SelectCommand.CommandText = "select menuid,menuname,tab from menu order by tab,menuname"
    DS.Reset()
    myAdapter.Fill(DS)
    If DS.Tables(0).Rows.Count > 0 Then
    For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
    Dim lbli As Literal
    lbli = New Literal
    Dim chk = New HtmlInputCheckBox()
    chk.ID = "f" & DS.Tables(0).Rows(i).Item(0)
    chk.Name = DS.Tables(0).Rows(i).Item(0)
    lbli.Text = DS.Tables(0).Rows(i).Item("tab").ToString.ToUpper & " - " & DS.Tables(0).Rows(i).Item(1) & "<br/>"
    Panel1.Controls.Add(chk)
    Panel1.Controls.Add(lbli)

    Next
    End If
    myConnection.Dispose()
    myAdapter.Dispose()
    DS.Dispose()
    End Sub


    Now when the button is click to save you check all the checkboxes and see which ones are checked.


    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click

    Dim myConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
    Dim sqlString As String = Nothing
    Dim myConnection As New MySqlConnection(myConnectionString)
    Dim cmd As MySqlCommand = New MySqlCommand(sqlstring, myConnection)
    Dim myAdapter As MySqlDataAdapter = New MySqlDataAdapter(sqlString, myConnection)
    Dim DS As DataSet = New DataSet
    myAdapter.SelectCommand.CommandText = "select menuid,menuname from menu"
    DS.Reset()
    myAdapter.Fill(DS)
    cmd.Connection.Open()
    If DS.Tables(0).Rows.Count > 0 Then
    For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
    Dim chkbox As System.Web.UI.HtmlControls.HtmlInputCheckBox = CType(Panel1.FindControl("f" & DS.Tables(0).Rows(i).Item(0)), System.Web.UI.HtmlControls.HtmlInputCheckBox)
    If chkbox.Checked Then
    sqlString = "INSERT INTO menuaccess (menuid,user) values (" & DS.Tables(0).Rows(i).Item(0) & ",'" & DropDownList1.SelectedValue & "')"
    cmd.CommandText = sqlstring
    Try
    cmd.ExecuteNonQuery()
    Catch ee As Exception
    End Try

    End If
    Next
    End If
    cmd.Connection.Close()
    myConnection.Dispose()
    myAdapter.Dispose()
    cmd.Dispose()
    DS.Dispose()
    End Sub

    Hope this helps.
     
    islandhopper8, Dec 17, 2008 IP