Matias aabye

Matias aabye

  • NA
  • 10
  • 2.9k

Radio Button Values into SQL

May 11 2013 10:37 PM
Hello Everyone. 

Really hope someone can help with this, as it is for a exam on monday! :) 

I got a page, where there are 40 radio buttons: 
My big problem is, how do i select the checked once, and insert them into database? 

Nestedrepeater.aspx
__________
<!-- start parent repeater -->
<asp:repeater id="parentRepeater" runat="server">
   <itemtemplate>
      <b>
          <%# DataBinder.Eval(Container.DataItem,"QuestionsName") %>
      </b><br>

      <!-- start child repeater -->
      <asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem)
      .Row.GetChildRows("myrelation") %>' runat="server">

         <itemtemplate>

             <asp:RadioButton GroupName="TEST"  ID="RadioButton1" Text='<%# DataBinder.Eval(Container.DataItem, "[\"QuizAnswer\"]")%>' 
                SelectedValue='<%# DataBinder.Eval(Container.DataItem, "[\"QuizAnswerid\"]")%>' runat="server" TextAlign="Right" /> <br />
             <asp:HiddenField ID="HiddenField1" Value='<%# DataBinder.Eval(Container.DataItem, "[\"Correct\"]")%>' runat="server" />
         </itemtemplate>
       
      </asp:repeater>
      <!-- end child repeater -->
           <br />
             <br />
   </itemtemplate>
</asp:repeater>
<!-- end parent repeater -->


Nedstedrepeater.aspx.cs
_______________________
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NestedRepeater
{
    public partial class NestedRepeater : System.Web.UI.Page
    {

        public NestedRepeater()
        {
            Page.Init += new System.EventHandler(Page_Init);
        }
        public void Page_Load(object sender, EventArgs e)
        {

        
         

            //Create the connection and DataAdapter for the Authors table.
            SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["QuizDataConnectionString1"].ConnectionString);
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from Questions", cnn);

            //Create and fill the DataSet.
            DataSet ds = new DataSet();
            cmd1.Fill(ds, "Questions");

            //Create a second DataAdapter for the Titles table.
            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from QuizAnswers", cnn);
            cmd2.Fill(ds, "QuizAnswers");

            //Create the relation bewtween the Authors and Titles tables.
            ds.Relations.Add("myrelation",
            ds.Tables["Questions"].Columns["Questionsid"],
            ds.Tables["QuizAnswers"].Columns["QuestionID"]);

            //Bind the Authors table to the parent Repeater control, and call DataBind.
            parentRepeater.DataSource = ds.Tables["Questions"];
            Page.DataBind();

            //Close the connection.
            cnn.Close();
        }
        private void Page_Init(object sender, EventArgs e)
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

       
        // Event when button is cliced
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["QuizDataConnectionString1 "].ConnectionString;
        cnn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from  Answers";
        cmd.Connection = cnn;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds, " Answers ");
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        DataRow drow = ds.Tables["Answers"].NewRow();
        drow["Name"] = Name.Text;
       drow["Telephone"] = Telephone.Text;
              drow["Email"] = Mail.Text;
// As you can see here, i got the field ready, just need to find the selected data to insert. 
              drow["Qs1"] = TextBox1.Text;
              drow["Qs2"] = TextBox1.Text;
              drow["Qs3"] = TextBox1.Text;
              drow["Qs4"] = TextBox1.Text;
              drow["Qs5"] = TextBox1.Text;
              drow["Qs6"] = TextBox1.Text;
              drow["Qs7"] = TextBox1.Text;
              drow["Qs8"] = TextBox1.Text;
              drow["Qs9"] = TextBox1.Text;
              drow["Qs10"] = TextBox1.Text;
              drow["Total_Correct_Answers"] = TextBox1.Text;
 
        ds.Tables["Answers "].Rows.Add(drow);
        da.Update(ds, " Answers ");
        string script = @"<script language=""javascript"">
        alert('Information have been Saved Successfully.......!!!!!.');
       </script>;";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myJScript1", script);
    }


        }
}
        }




Answers (2)