hi,
there, i am trying to connect the dropdownlist and gridview to the sql database. I am inserting radiobutton into the gridview. The whole thing goes like this, the dropdownlist is checked and once the radiobutton is checked, and clicked submitted, the next page must show only the checked data. I am getting alot of error. I have also included the VB behind code.
<script language="javascript" type="text/javascript">
// <!CDATA[
function IMG1_onclick() {
}
function TABLE1_onclick() {
}
function Submit1_onclick() {
}
function HR1_onclick() {
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" cellpadding="0" cellspacing="0" align="center">
<tr>
<td style="height: 21px; width: 77px;">
<table width="800" id="TABLE1" onclick="return TABLE1_onclick()" align=center >
<tr>
<td align="left" colspan="3" style="width: 790px">
</td>
</tr>
<tr>
<td align="center" colspan="3" style="width: 790px; height: 23px;">
<asp:DropDownList ID="ddlbatches" runat="server" AutoPostBack="True"
Visible="False">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" Text="Load Students"
Width="95px" Visible="False" />
Delivery Boy <asp:DropDownList ID="ddlsubjects" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td width: 746px;" colspan="3" style="height: 147px; width: 790px;" align="center">
<asp:GridView ID="gvattendance" runat="server" AutoGenerateColumns="False" DataKeyNames="Receiver" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="497px" PageSize="5">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="Receiver Name" HeaderText="Receiver" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Poscode" HeaderText="Poscode" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="State" HeaderText="State" />
<asp:BoundField DataField="Contact_Num" HeaderText="Contact Number" />
<asp:TemplateField HeaderText="Assign Task">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
<tr>
<td align="center" colspan="3" style="width: 790px; height: 21px">
<asp:Button ID="Button1" runat="server" Text="Submit Attendance" Width="222px" /></td>
</tr>
</table>
</td>
</tr>
</table>
[B]VB behind code[/B]
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.DateTime
Partial Class _Default
Inherits System.Web.UI.Page
Dim str As String = ConfigurationManager.ConnectionStrings("mycon").ConnectionString
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim rdr As SqlDataReader
Dim sql As String
Dim obj As general
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Me.lblname.Text = Session("ses_dname")
loadbatches()
loadsubjects()
End If
End Sub
Private Sub load_gvattendance(ByVal bid As String)
con = New SqlConnection(str)
con.Open()
sql = "select * from tbstudent where bid=" + bid.ToString
cmd = New SqlCommand(sql, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New Data.DataSet()
da.Fill(ds)
gvattendance.DataSource = ds
gvattendance.DataBind()
End Sub
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvattendance.RowCommand
Dim cid As Integer
If (e.CommandName = "Select") Then
cid = gvattendance.Rows(e.CommandArgument.ToString).Cells(0).Text.ToString
Response.Redirect("course.aspx?cid=" + cid.ToString + "&action=update")
End If
End Sub
Protected Sub gvattendance_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvattendance.PageIndexChanging
gvattendance.PageIndex = e.NewPageIndex
gvattendance.DataBind()
load_gvattendance(ddlbatches.SelectedValue)
End Sub
Private Sub loadbatches()
con = New SqlConnection(str)
con.Open()
sql = "select bid,bname from tbbatch "
cmd = New SqlCommand(sql, con)
rdr = cmd.ExecuteReader
ddlbatches.DataSource = rdr
ddlbatches.DataTextField = "bname"
ddlbatches.DataValueField = "bid"
ddlbatches.DataBind()
con.Close()
End Sub
Private Sub loadsubjects()
con = New SqlConnection(str)
con.Open()
sql = "select sbid,sbname from tbsubject"
cmd = New SqlCommand(sql, con)
rdr = cmd.ExecuteReader
ddlsubjects.DataSource = rdr
ddlsubjects.DataTextField = "sbname"
ddlsubjects.DataValueField = "sbid"
ddlsubjects.DataBind()
con.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each row As GridViewRow In gvattendance.Rows
' Access the CheckBox
Dim cb1 As CheckBox = row.FindControl("RadioButton1")
Dim cb2 As CheckBox = row.FindControl("RadioButton2")
If cb1 IsNot Nothing Then
If cb1.Checked = True Then
Dim sid As Integer = Convert.ToInt32(gvattendance.DataKeys(row.RowIndex).Value)
insertatt(sid, "present")
End If
End If
If cb2 IsNot Nothing Then
If cb2.Checked = True Then
Dim sid As Integer = Convert.ToInt32(gvattendance.DataKeys(row.RowIndex).Value)
insertatt(sid, "absent")
End If
End If
Next
obj = New general()
obj.AlertRedirect(Page, "Attendances Submitted", "attendance.aspx")
End Sub
Private Sub insertatt(ByVal sid As String, ByVal atype As String)
Dim dt As System.DateTime
dt = New System.DateTime
dt = DateTime.Now
Dim dd As String = dt.Day.ToString()
Dim mm As String = dt.Month.ToString
Dim yyyy As String = dt.Year.ToString
Dim d2 As String = mm + "/" + dd + "/" + yyyy
con = New SqlConnection(str)
con.Open()
sql = "insert into tbatt(sid,bid,sbid,adate,atype) values"
sql = sql + "('" + sid + "','" + ddlbatches.SelectedValue + "','" + ddlsubjects.SelectedValue + "','" + d2 + "','" + atype + "')"
cmd = New SqlCommand(sql, con)
cmd.ExecuteNonQuery()
con.Close()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
load_gvattendance(ddlbatches.SelectedValue)
End Sub
End Class