Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi i have 4 options with check box or radio button now if i select only one option from these 4 options the data which is selected should sit in the database can any one help me out in this.
i am stuck up with this badly can anyone give me some examples
Posted
Updated 20-Nov-11 19:34pm
v3
Comments
rajeevcapgeminiindia 21-Nov-11 0:35am    
Which data should sit/save into the database on the selection.
Please clarify your question.
jawad khadri 21-Nov-11 0:43am    
data checked in check box should sit in database
Sergey Alexandrovich Kryukov 21-Nov-11 0:36am    
Be careful with questions. No question mark. What exactly the question is? Where did you stuck?
With questions like yours, you risk getting an answer like "Yes, someone can", because this is what you're asking about.

Also, please use complete punctuation, capitalization, correct spelling, no abbreviations.

Thank you,
--SA

Hi,
To get values that are Checked in Check-box List you can Use Following Function....
C#
public static string GetCheckboxSelectedValues(CheckBoxList chk)
  {
      string values = "";
      for (int i = 0; i < chk.Items.Count; i++)
      {
          if (chk.Items[i].Selected)
          {
              values += chk.Items[i].Value + ",";
          }
      }
      values = values.TrimEnd(',');
      return values;
  }

Just Pass CheckBoxList Id..it Will Return Checked Values In Comma Separate string Format
 
Share this answer
 
There are a number of ways to tackle.

Off hand, here are two approaches -

1) Create 4 boolean columns in the database and fill in true for the checked radio button and false for others.

2) Create just one column. Create four unqiue identifiers (one for each radiobutton e.g. 1,2,3 and 4). Fill the column with the unique identifier value after figuring out out which of the selected / checked maps to which identifier.
 
Share this answer
 
Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page
Dim DBConn As New SqlConnection("Data Source=ZETEONWS05;Initial Catalog=testingcheck;Integrated Security=True;")
Dim DBCmd As SqlCommand

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DBConn.Open()
End Sub

Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
Try

Dim SubNLetter1 As Boolean = False
Dim SubNLetter2 As Boolean = False
Dim SubNLetter3 As Boolean = False
Dim SubNLetter4 As Boolean = False
For Each LItems As ListItem In cblNewsLetter.Items
If LItems.Selected = True Then
Select Case LItems.Value
Case "1"
SubNLetter1 = True
Case "2"
SubNLetter2 = True
Case "3"
SubNLetter3 = True
Case "4"
SubNLetter4 = True
End Select
End If
Next

Dim SQLQuery As String = "INSERT INTO Subscription (FirstName, LastName, Address, EMail, CellNo, Country, LangPref, SubNLetter1, SubNLetter2, SubNLetter3, SubNLetter4) VALUES (@FirstName, @LastName, @Address, @EMail, @CellNo, @Country, @LangPref, @SubNLetter1, @SubNLetter2, @SubNLetter3, @SubNLetter4)"
DBCmd = New SqlCommand(SQLQuery, DBConn)
DBCmd.Parameters.Add("@FirstName", SqlDbType.Char, 50).Value = txtFirstName.Text
DBCmd.Parameters.Add("@LastName", SqlDbType.Char, 50).Value = txtLastName.Text
DBCmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtAddress.Text
DBCmd.Parameters.Add("@EMail", SqlDbType.Char, 50).Value = txtEMail.Text
DBCmd.Parameters.Add("@CellNo", SqlDbType.VarChar, 50).Value = txtCellNo.Text
DBCmd.Parameters.Add("@Country", SqlDbType.Char, 50).Value = ddlCountry.SelectedValue.ToString
DBCmd.Parameters.Add("@LangPref", SqlDbType.Char, 50).Value = rbVersion.SelectedItem.ToString
DBCmd.Parameters.Add("@SubNLetter1", SqlDbType.Char, 50).Value = SubNLetter1
DBCmd.Parameters.Add("@SubNLetter2", SqlDbType.Char, 50).Value = SubNLetter2
DBCmd.Parameters.Add("@SubNLetter3", SqlDbType.Char, 50).Value = SubNLetter3
DBCmd.Parameters.Add("@SubNLetter4", SqlDbType.Char, 50).Value = SubNLetter4
DBCmd.ExecuteNonQuery()
DBCmd.Dispose()
DBCmd = Nothing

Catch ex As Exception
Response.Write(ex)
End Try

End Sub

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
DBConn.Close()
DBConn = Nothing
End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900