Dynamically add checkbox to ASP.NET table row - asp.net

I am trying to add a checkboxlist and then add a new checkbox to every row in an ASP.NET table:
Dim cbList As New CheckBoxList
cbList.ID = "cb_list"
Dim c1 As New TableCell
Dim c2 As New TableCell
Dim r As New TableRow
c1.Controls.Add(New LiteralControl(dr.GetString(1)))
c2.Controls.Add(cbList.Items.Add("new cb")) 'error: expression does return a value
r.Cells.Add(c1)
r.Cells.Add(c2)
modaltable.Rows.Add(r)
The c2.Controls line returns the following error:
"expression does return a value"
I can't seem to get round this problem. Any help is appreciated!

Related

How to use added table in tablecell asp.net

I added the table in TableCell
Dim rSkala As New TableRow()
Dim cSkala As New TableCell()
cSkala.Controls.Add(New Table())
rSkala.Cells.Add(cSkala)
How can I use this table now? I want add to the table rows and cells, but this table haven't name
You must assign programmatically the ID of the control:
Dim rSkala As New TableRow()
Dim cSkala As New TableCell()
Dim newTable as New Table()
newTable.ID = "myID"
cSkala.Controls.Add(newTable)
rSkala.Cells.Add(cSkala)
At this point your table has an ID to be referenced (es. with FindControl("myID"))

If checkbox Checked then add to datatable from Gridview

I am trying check whether a checkbox is checked in a gridview and if it is checked to add it to the datatable.
However I am getting an error when the checkbox is unchecked for the row:
There is no row at position 1.
Here is my code:
'Creates a new datatable
Dim dtQuestions As New DataTable("QuestionsData")
'Add columns to datatable
For Each cell As TableCell In example.HeaderRow.Cells
dtQuestions.Columns.Add(cell.Text)
Next
For Each row As GridViewRow In example.Rows
Dim chkTest As CheckBox = CType(row.FindControl("chkTest"), CheckBox)
If chkTest.Checked = True Then
dtQuestions.Rows.Add()
For i As Integer = 0 To row.Cells.Count - 1
Try
dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text
Catch ex As Exception
End Try
Next
Else
'Do not add it to Datatable
End If
Next
I am getting the error on this code:
dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text
I do not know how to fix this.
If you want to add a row to a DataTable you should write this code
For Each row As GridViewRow In example.Rows
Dim chkTest As CheckBox = CType(row.FindControl("chkTest"), CheckBox)
If chkTest.Checked = True Then
Dim tableRow = dtQuestions.NewRow()
For i As Integer = 0 To row.Cells.Count - 1
tableRow(i) = row.Cells(i).Text
Next
dtQuestions.Rows.Add(tableRow)
End If
Next
Notice that I have removed the empty try/catch. Don't do it because it just hides your problems.

Adding row in asp.net table

I have the following code in my .aspx file
<asp:Table ID="tabStudent" runat="server"></asp:Table>
I declared all variables which I used below. I wrote the following code snippet in the page load event. But it only shows only one row which having last row in my database. But when I am using message box inside this while loop to print those values I am getting all the rows.
Reader = Command.ExecuteReader()
While Reader.Read()
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While
what is wrong with this code?
You are not creating new controls in the loop but you're always reusing the same.
While Reader.Read()
TableRow = New TableRow()
lblRollNo = New Label()
tcRollNo = New TableCell()
' and so on ...
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While

ORA-01036: illegal variable name/number error on parameter in SELECT

I am trying to populate a series of dropdown lists in a GridView. In the RowDataBound event, I call based on other info in the gridviewrow to get the items for populating dropdowns in the row. I am getting "ORA-01036: illegal variable name/number error on parameter" on the following code.
Dim rowTemp As GridViewRow = ddlItemDesc.Parent.Parent
Dim lblYear As Label = rowTemp.FindControl("lblSchoolYear")
Dim strYear As String = lblYear.Text.Trim()
Dim strSelect As String = "SELECT FOO.BAR.PRODUCT_CODE || ' :: ' || FOO.BAR.PRODUCT_DESC AS txt, FOO.BAR.PRODUCT_KEY AS val, :currentYear AS CURRENT_YEAR FROM FOO.BAR WHERE (FOO.BAR.PRODUCT_KEY=:itemKey)"
Dim daTemp As New OracleDataAdapter(strSelect, oConn)
daTemp.SelectCommand.Parameters.Add("currentYear", CDate(strYear))
daTemp.SelectCommand.Parameters.Add("itemKey", strItemVal(0))
Dim dtProd As New DataTable
daTemp.Fill(dtProd)
If I remove ":currentYear AS CURRENT_YEAR" from the SELECT statement, it runs fine. I use the same statement in another SELECT from a join of other tables with no problem.
The strYear comes from a label in the gridviewrow and I have checked that it has data and that it is valid for CDate (I've also tried it simply as a string with the same results). StrItemVal(0) comes from another dropdownlist (the one that is successfully populated using :currentYear being passed in). This code only gets called if that dropdown list has a value in it that is selected.

SqlDataSource.Select()? How do I use this? (ASP.net)

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?
I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.
This puts the result query in to a DataTable.
DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;
foreach (DataRow dr in dt.Rows)
{
// Do something here IE grab the value of the first column
value = dr[0];
}
Repying to last question in comment:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
I was getting crazy trying to do this simple operation:
retrieving data from sqldatasource and put it into variables that I can manipulate.
At the end, Here the working behind code to do this for VB.NET:
Dim DV As New DataView()
Dim DataTable As New DataTable()
Dim SqlDataSource1 As New SqlDataSource()
Dim VALUE As String
SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString
SqlDataSource1.SelectCommand = "SELECT * from Table"
DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
DataTable = DV.ToTable()
For Each riga As DataRow In DataTable.Rows
VALUE = riga("table_name").ToString
Next
the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.
ENJOY

Resources