I want to be able to select an option from a drop-down and then display a grid with checkbox option and save the selected options in a database but my code is not working please check.
The goal is to select a subject from the table "subjects" through a dropdown and then display the corresponding CourseOutcomes (which are multiple lines)
in the form of a grid view with checkboxes on the side which then on selection will save the data into another table called savedCLO.
public partial class ModView : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\v11.0;Initial Catalog=facultylog;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
{
String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
String strQuery = "select Subject_Name from subjects";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Subject_Name";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
SqlCommand cmd = new SqlCommand("select CourseOutcomes from CLO", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
var checkbox = gvrow.FindControl("CheckBox1") as CheckBox;
if (checkbox.Checked)
{
var lblId = gvrow.FindControl("Label1") as Label;
var lblSubjectName = gvrow.FindControl("Label2") as Label;
var lblCourseOutcomes = gvrow.FindControl("Label3") as Label;
SqlCommand cmd = new SqlCommand("insert into savedCLO (Id, SubjectName, CourseOutcomes) values (@Id, @SubjectName, @CourseOutcomes)", con);
cmd.Parameters.AddWithValue("Id", lblId.Text);
cmd.Parameters.AddWithValue("SubjectName", lblSubjectName.Text);
cmd.Parameters.AddWithValue("CourseOutcomes", lblCourseOutcomes.Text);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
refreshdata();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager.ConnectionStrings["facultylogConnectionString"].ConnectionString;
String strQuery = "select CourseOutcomes from CLO where" + " SubjectName = @Subject_Name";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@Subject_Name", DropDownList1.SelectedItem.Value);
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
refreshdata();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
ASPX
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False"
="" backcolor="White" bordercolor="#DEDFDE" borderstyle="None" borderwidth="1px" cellpadding="4" forecolor="Black" gridlines="Vertical" style="margin-top: 142px">
<alternatingrowstyle backcolor="White">
<columns>
<asp:templatefield headertext="ID">
<edititemtemplate>
<asp:textbox id="TextBox1" runat="server" text="<%# Bind("Id") %>">
<itemtemplate>
<asp:label id="Label1" runat="server" text="<%# Bind("Id") %>">
<asp:templatefield headertext="Subject Name">
<edititemtemplate>
<asp:textbox id="TextBox2" runat="server" text="<%# Bind("SubjectName") %>">
<itemtemplate>
<asp:label id="Label2" runat="server" text="<%# Bind("SubjectName") %>">
<asp:templatefield headertext="Course Outcomes">
<edititemtemplate>
<asp:textbox id="TextBox3" runat="server" text="<%# Bind("CourseOutcomes") %>">
<itemtemplate>
<asp:label id="Label3" runat="server" text="<%# Bind("CourseOutcomes") %>">
<asp:templatefield headertext="Select Applicable">
<edititemtemplate>
<asp:checkbox id="CheckBox1" runat="server">
<itemtemplate>
<asp:checkbox id="CheckBox1" runat="server">
<footerstyle backcolor="#CCCC99">
<headerstyle backcolor="#6B696B" font-bold="True" forecolor="White">
<pagerstyle backcolor="#F7F7DE" forecolor="Black" horizontalalign="Right">
<rowstyle backcolor="#F7F7DE">
<selectedrowstyle backcolor="#CE5D5A" font-bold="True" forecolor="White">
<sortedascendingcellstyle backcolor="#FBFBF2">
<sortedascendingheaderstyle backcolor="#848384">
<sorteddescendingcellstyle backcolor="#EAEAD3">
<sorteddescendingheaderstyle backcolor="#575357">
<asp:dropdownlist id="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:button id="Button1" runat="server" onclick="Button1_Click"
="" text="Save to Database">
What I have tried:
I have tried the above code but it does not work.
The goal is to select a subject from the table "subjects" through a dropdown and then display the corresponding CourseOutcomes (which are multiple lines)
in the form of a grid view with checkboxes on the side which then on selection will save the data into another table called savedCLO.
Please tell me how to fix this code, I don't want to change the way I'm doing it I just want to fix the current code. I don't want alternative methods because I tried so many and they are all not working out thanks at this point it would be easier to just fix this code. If you do help thanks it means a lot and I'm struggling with this whole project.