I have a table on my page. The first column contains checkboxes for each row of data. The other columns are bound to my database. I need to run an SQL statement on the rows that are checked without changing data in rows that aren't checked. Here's my table:
I
have a drop-down list that allows the user to determine which SQL statement will be run. Here's the code in the If (IsPost) block:
if (Choice.Equals("DDLoption1"))
{
foreach (var row in db.Query(selectTable1))
{
if (checkbox.IsEmpty())
{
//do nothing
}
else
{
db.Execute(SQLstatement1);
}
}
}
What happens: If all rows are checked, the statement is applied to the data n times, where n = number of data rows. If some rows are checked, I get the same result (n times on checked and unchecked rows). If no rows are checked, no rows are updated.
I need it to update only the checked rows. I would really appreciate your help on this. Thanks!
Sir Codalot
Member
7 Points
10 Posts
Need to run SQL statement only on table rows that have a checked checkbox. (cshtml)
Feb 13, 2012 07:21 PM|LINK
I have a table on my page. The first column contains checkboxes for each row of data. The other columns are bound to my database. I need to run an SQL statement on the rows that are checked without changing data in rows that aren't checked. Here's my table:
<table id="table">
<thead>
<tr>
<th><input type='checkbox' name='checkall' onclick='checkedAll(frm1);' checked="checked"></th>
<th>column1heading</th>
<th>column2heading</th>
<th>column3heading</th>
</tr>
</thead>
<tbody>
@foreach (var row in db.Query(selectTable1))
{
<tr id="tablerows">
<td><input id="Checkbox1" type="checkbox" name="checkbox" checked="checked"/></td>
<td>column1</td>
<td>column2</td>
<td>column3</td>
<tr>
}
</tbody>
</table>
I have a drop-down list that allows the user to determine which SQL statement will be run. Here's the code in the If (IsPost) block:
if (Choice.Equals("DDLoption1"))
{
foreach (var row in db.Query(selectTable1))
{
if (checkbox.IsEmpty())
{
//do nothing
}
else
{
db.Execute(SQLstatement1);
}
}
}
What happens: If all rows are checked, the statement is applied to the data n times, where n = number of data rows. If some rows are checked, I get the same result (n times on checked and unchecked rows). If no rows are checked, no rows are updated.
I need it to update only the checked rows. I would really appreciate your help on this. Thanks!
GmGregori
Contributor
5564 Points
749 Posts
Re: Need to run SQL statement only on table rows that have a checked checkbox. (cshtml)
Feb 13, 2012 08:34 PM|LINK
I think that isn't so easy accomplish your goals.
You should distinguish any checkbox with an id that links it to an record's id, e.g.
and then examine the checkboxes extracting those checked and executing the sql statements against the linked records.
At this link http://msdn.microsoft.com/en-us/library/hh145668(v=vs.99).aspx#sec9 you can find a lesson that explains just this procedure.
Sir Codalot
Member
7 Points
10 Posts
Re: Need to run SQL statement only on table rows that have a checked checkbox. (cshtml)
Feb 19, 2012 01:48 PM|LINK
I was able to adapt the example in the lesson suggested by GmGregori and now it works. Thanks a lot!!!