0

I am trying to save radio button selected value to database; not sure how to do that. I found the C# code shown here on Google, I need to modify it so that if three sets of radio buttons are set, I need to save all radio button selected values to database using foreach.

<table id='attendence'>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60079">
  </td>
  <td>
    <input type="text" readonly="readonly" value="ARUNA S">
  </td>
  <th>
    <input type="radio" class="present" name="Present1" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present1" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present1" value="Leave">
  </th>
</tr>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60080">
  </td>
  <td>
    <input type="text" readonly="readonly" value="ASHWINI S M">
  </td>
  <th>
    <input type="radio" class="present" name="Present2" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present2" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present2" value="Leave">
  </th>
</tr>
<tr>
  <td>
    <input type="text" readonly="readonly" value="12KQA60220">
  </td>
  <td>
    <input type="text" readonly="readonly" value="Das">
  </td>
  <th>
    <input type="radio" class="present" name="Present3" value="Present">
  </th>
  <th>
    <input type="radio" class="absent" name="Present3" value="Absent">
  </th>
  <th>
    <input type="radio" class="leave" name="Present3" value="Leave">
  </th>
</tr>
</table>

C#

string sql = "INSERT INTO MyTable (Col1, Col2) VALUES (@attendence, @username)"; 

using (MySqlConnection con = new MySqlConnection(connectionString))
{  
    int retvalue;
    con.Open();   

    foreach (DataRow row in myTable.Rows)   
    {      
        MySqlCommand myCommand = new MySqlCommand();
        myCommand.Connection = con;      
        myCommand.CommandText = sql;      
        myCommand.Parameters.AddWithValue("@attendence", r["attendence"]);      
        myCommand.Parameters.AddWithValue("@username", r["username"]);              
        retvalue = myCommand.ExecuteNonQuery();   
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shaik
  • 930
  • 1
  • 20
  • 48
  • @ParthTrivedi am new to this the similar i have done in PHP wehre i use foreach to get all the radio button value the above there are 3 radio button in a group absent present Leave the same radio button vl be generated dynamically wat i need is i want to save only the selected value of the radio button of each group – Shaik Dec 15 '15 at 10:19
  • do you use asp.net mvc? – Parth Trivedi Dec 15 '15 at 10:23
  • core aspx page? Then server Grid control is the best way. – Parth Trivedi Dec 15 '15 at 10:24
  • im not sure u understood my question the radio button group are dynamically generated so how can i use the grid control – Shaik Dec 15 '15 at 10:26
  • @Shaik Kaddadi no 6o bhai tame – Ankit Kathiriya Dec 15 '15 at 10:27
  • @AnkitKathiriya sorry i dint get u – Shaik Dec 15 '15 at 10:28
  • May be this will help you http://www.codeproject.com/Articles/13050/RadioButtons-inside-a-GridView-control – Parth Trivedi Dec 15 '15 at 10:30
  • @ParthTrivedi thanks for ur support i have a quick question how can the dynamically incremented radio button selected value can be saved – Shaik Dec 15 '15 at 10:33
  • @ParthTrivedi last question can you suggest me how to save radio button selected value that are dynamically created – Shaik Dec 15 '15 at 10:39
  • http://stackoverflow.com/questions/308301/reading-the-selected-value-from-aspradiobuttonlist-using-jquery this will help you – Parth Trivedi Dec 15 '15 at 10:42
  • Also try http://stackoverflow.com/questions/15700148/getting-value-from-html-radio-button-in-aspx-c?answertab=active#tab-top – Parth Trivedi Dec 15 '15 at 10:44

1 Answers1

0

Give id/name for the . you can use that id for getting the username and for getting present/absent.leave status.

<input id="username1" type="text" readonly="readonly" value="ARUNA S">
<input id="radio1" type="radio" class="present" name="Present2" value="Present">
<input  id="radio2" type="radio" class="absent" name="Present2" value="Absent">
<input  id="radio3" type="radio" class="leave" name="Present2" value="Leave">

get username as

 username1.text

get status as-

if(radio1.checked)
    attendance=radio1.value
else if(radio2.checked)
    attendance=radio2.value
else if(radio3.checked)
    attendance=radio3.value
Sahi
  • 1,454
  • 1
  • 13
  • 32