This a copy of an unanwered post on asp.net forum
I have been hung up on this now for a while.
I have a gv that has multiple select checkboxes and a dropdownlist. In my codebehind, I captured the results and verified that they are collected.... see Part 1 in below code. I finally have this working from two tutorials.
Now for the problem that I am still having is getting this results into the another table in the database. SEE Part 2
I need to add the dropdownlist results to TestName
And the value of the PK (int) and the on/off (true/false) for the remaining checkbox
Field Properties are :
TestName varchar(50)
test1ID int
TestDefault Bit (True/False)
I know I need a For Each statement with the parameters value, but here is where I am totally lost. Do I need to repeat my IF Then statement for the TestDefault results. I have tried many ways since yesterday and I cannot get the info saved into the the table.... sql 2005. An example would be awesome.
I am running out of time on getting the test resolved.
Thanks in advance.
Regards!
Code below grabs the gridview check/selected items and a button1.click event handler.
Protected Sub Botton1_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
**************** PART1 **************************
Dim gvIDs As String = ""
Dim ddIDs As String = ""
Dim chkBox As Boolean = False
Dim chkBox1 As Boolean = False
'Navigate through each row in the GridView for checkbox items
ddIDs = DropDownList1.SelectedValue.ToString
For Each gv As GridViewRow In GridView1.Rows
Dim addChkBxItem As CheckBox = CType(gv.FindControl("delCheckBox"), CheckBox)
Dim addChkBx1Item As CheckBox = CType(gv.FindControl("CheckBox1"), CheckBox)
If addChkBxItem.Checked Then
chkBox = True
gvIDs = CType(gv.FindControl("TestID"), Label).Text.ToString
Response.Write(ddIDs + " " + gvIDs + " ")
If addChkBx1Item.Checked Then
chkBox1 = True
Response.Write("Defaut Item")
End If
Response.Write("<br />")
End If
Next
******************** Part 2 *****************************************************
Dim cn As SqlConnection = New SqlConnection(SqlDataSource1.ConnectionString)
If chkBox Then
Try
Dim insertSQL As String = "INSERT INTO testwrite (TestLast, test1ID, TestDefault) VALUES (@TestLast, @test1ID, @TestDefault)"
Dim cmd As SqlCommand = New SqlCommand(insertSQL, cn)
cmd.Parameters.AddWithValue("@TestLast", DropDownList1.SelectedValue.ToString)
cmd.Parameters.AddWithValue("@test1ID", CType(gv.FindControl("TestID"), Label).Text.ToString)
cmd.Parameters.AddWithValue("@TestDefault, CType(gv.FindControl("CheckBox1"), CheckBox))
cn.Open()
cmd.ExecuteNonQuery()
Catch err As SqlException
Response.Write(err.Message.ToString)
Finally
cn.Close()
End Try
End If
Bob B.
I have been hung up on this now for a while.
I have a gv that has multiple select checkboxes and a dropdownlist. In my codebehind, I captured the results and verified that they are collected.... see Part 1 in below code. I finally have this working from two tutorials.
Now for the problem that I am still having is getting this results into the another table in the database. SEE Part 2
I need to add the dropdownlist results to TestName
And the value of the PK (int) and the on/off (true/false) for the remaining checkbox
Field Properties are :
TestName varchar(50)
test1ID int
TestDefault Bit (True/False)
I know I need a For Each statement with the parameters value, but here is where I am totally lost. Do I need to repeat my IF Then statement for the TestDefault results. I have tried many ways since yesterday and I cannot get the info saved into the the table.... sql 2005. An example would be awesome.
I am running out of time on getting the test resolved.
Thanks in advance.
Regards!
Code below grabs the gridview check/selected items and a button1.click event handler.
Protected Sub Botton1_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
**************** PART1 **************************
Dim gvIDs As String = ""
Dim ddIDs As String = ""
Dim chkBox As Boolean = False
Dim chkBox1 As Boolean = False
'Navigate through each row in the GridView for checkbox items
ddIDs = DropDownList1.SelectedValue.ToString
For Each gv As GridViewRow In GridView1.Rows
Dim addChkBxItem As CheckBox = CType(gv.FindControl("delCheckBox"), CheckBox)
Dim addChkBx1Item As CheckBox = CType(gv.FindControl("CheckBox1"), CheckBox)
If addChkBxItem.Checked Then
chkBox = True
gvIDs = CType(gv.FindControl("TestID"), Label).Text.ToString
Response.Write(ddIDs + " " + gvIDs + " ")
If addChkBx1Item.Checked Then
chkBox1 = True
Response.Write("Defaut Item")
End If
Response.Write("<br />")
End If
Next
******************** Part 2 *****************************************************
Dim cn As SqlConnection = New SqlConnection(SqlDataSource1.ConnectionString)
If chkBox Then
Try
Dim insertSQL As String = "INSERT INTO testwrite (TestLast, test1ID, TestDefault) VALUES (@TestLast, @test1ID, @TestDefault)"
Dim cmd As SqlCommand = New SqlCommand(insertSQL, cn)
cmd.Parameters.AddWithValue("@TestLast", DropDownList1.SelectedValue.ToString)
cmd.Parameters.AddWithValue("@test1ID", CType(gv.FindControl("TestID"), Label).Text.ToString)
cmd.Parameters.AddWithValue("@TestDefault, CType(gv.FindControl("CheckBox1"), CheckBox))
cn.Open()
cmd.ExecuteNonQuery()
Catch err As SqlException
Response.Write(err.Message.ToString)
Finally
cn.Close()
End Try
End If
Bob B.