[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

MULTIPLE Selected checkbox value should be stored

I need to store , checkboxes.text value , the below works only for a single selection ,

If CheckBox1.Checked Then
            chkboxemp.Value = CheckBox1.Text.ToString()
        ElseIf CheckBox2.Checked Then
            chkboxemp.Value = CheckBox2.Text.ToString()
        ElseIf CheckBox3.Checked Then
            chkboxemp.Value = CheckBox3.Text.ToString()
        End If
 
how to  store checkboxes text value according to the selection of checkboxes .i mean if user selects two checkboxes means those both values should be stored and so on....

and

am trying to store the current date and ipaddress using this code

 Dim postdate As DateTime = DateTime.Now.ToString()
 Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")

But i get this error
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.


Dim con As SqlConnection
        Dim constr As String = ConfigurationManager.AppSettings("conxnstr")
        con = New SqlConnection(constr)
        con.Open()
        Dim cmd1 As New SqlCommand("sp_emp", con)
        cmd1.CommandType = CommandType.StoredProcedure
        Dim name As New SqlParameter("@name", SqlDbType.NVarChar, 100)
        name.Value = TextBox2.Text()
        Dim chkboxemp As New SqlParameter("@emp", SqlDbType.NVarChar, 300)
        If CheckBox1.Checked Then
            chkboxemp.Value = CheckBox1.Text.ToString()
        ElseIf CheckBox2.Checked Then
            chkboxemp.Value = CheckBox2.Text.ToString()
        ElseIf CheckBox3.Checked Then
            chkboxemp.Value = CheckBox3.Text.ToString()
        End If
        Dim postdate As DateTime = DateTime.Now.ToString()
        Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")
        cmd1.Parameters.Add(name)
        cmd1.Parameters.Add(chkboxemp)
        cmd1.Parameters.Add(postdate)
        cmd1.Parameters.Add(ipaddress)
        Dim comment As Integer = cmd1.ExecuteNonQuery()
        If comment = 1 Then
            Label1.Text = "Your Value is Added"
        Else
            Label1.Text = "Already Exists"
        End If
        con.Close()
 
<form id="form1" runat="server">
    <div>
        <br />
        Name<asp:TextBox ID="TextBox2" runat="server" Style="left: 40px; position: relative;
            top: -1px"></asp:TextBox>
        <br />
        <br />
       Type: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox1" runat="server" Text="Employee"/>         
        <asp:CheckBox ID="CheckBox2" runat="server" Text="Manager"/>
        <asp:CheckBox ID="CheckBox3" runat="server" Text="Ceo"/>
        <br />
        <br />
          <asp:Label ID="Label1" runat="server" Style="left: 156px; position: relative; top: -5px"
            Text="Label"></asp:Label><asp:Button ID="Button1" runat="server" Style="left: -89px; position: relative; top: 56px"
            Text="Button" /></div>   
    </form>

Open in new window

0
Rajar Ahmed
Asked:
Rajar Ahmed
  • 4
  • 4
1 Solution
 
Pratima PharandeCommented:
You mean to say if 3 check boxes seleted then 3 records need to insert then try somthing like this
 Dim con As SqlConnection
        Dim constr As String = ConfigurationManager.AppSettings("conxnstr")
        con = New SqlConnection(constr)
        con.Open()
        Dim cmd1 As New SqlCommand("sp_emp", con)
        cmd1.CommandType = CommandType.StoredProcedure
        Dim name As New SqlParameter("@name", SqlDbType.NVarChar, 100)
        name.Value = TextBox2.Text()
        Dim postdate As DateTime = DateTime.Now.ToString()
        Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")
 
        Dim chkboxemp As New SqlParameter("@emp", SqlDbType.NVarChar, 300)
        If CheckBox1.Checked Then
            chkboxemp.Value = CheckBox1.Text.ToString()
            cmd1.Parameters.Add(name)
            cmd1.Parameters.Add(chkboxemp)
            cmd1.Parameters.Add(postdate)
            cmd1.Parameters.Add(ipaddress)
            Dim comment As Integer = cmd1.ExecuteNonQuery()
            If comment = 1 Then
                Label1.Text = "Your Value is Added"
            Else
                Label1.Text = "Already Exists"
            End If
        ElseIf CheckBox2.Checked Then
            chkboxemp.Value = CheckBox1.Text.ToString()
            cmd1.Parameters.Add(name)
            cmd1.Parameters.Add(chkboxemp)
            cmd1.Parameters.Add(postdate)
            cmd1.Parameters.Add(ipaddress)
            Dim comment As Integer = cmd1.ExecuteNonQuery()
            If comment = 1 Then
                Label1.Text = "Your Value is Added"
            Else
                Label1.Text = "Already Exists"
            End If
        ElseIf CheckBox3.Checked Then
            chkboxemp.Value = CheckBox1.Text.ToString()
            cmd1.Parameters.Add(name)
            cmd1.Parameters.Add(chkboxemp)
            cmd1.Parameters.Add(postdate)
            cmd1.Parameters.Add(ipaddress)
            Dim comment As Integer = cmd1.ExecuteNonQuery()
            If comment = 1 Then
                Label1.Text = "Your Value is Added"
            Else
                Label1.Text = "Already Exists"
            End If
        End If
       
        
        con.Close()

Open in new window

0
 
Pratima PharandeCommented:

 Dim con As SqlConnection
        Dim constr As String = ConfigurationManager.AppSettings("conxnstr")
        con = New SqlConnection(constr)
        con.Open()
       
        Dim name As New SqlParameter("@name", SqlDbType.NVarChar, 100)
        name.Value = TextBox2.Text()
        Dim postdate As DateTime = DateTime.Now.ToString()
        Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")
        Dim comment As Integer
        Dim chkboxemp As New SqlParameter("@emp", SqlDbType.NVarChar, 300)
        If CheckBox1.Checked Then
            Dim cmd1 As New SqlCommand("sp_emp", con)
            cmd1.CommandType = CommandType.StoredProcedure
            chkboxemp.Value = CheckBox1.Text.ToString()
            cmd1.Parameters.Add("@name", SqlDbType.NVarChar).Value = name
            cmd1.Parameters.Add("@emp", SqlDbType.NVarChar).Value = chkboxemp
            cmd1.Parameters.Add("@postdate", SqlDbType.NVarChar).Value = postdate
            cmd1.Parameters.Add("@ipaddress", SqlDbType.NVarChar).Value = ipaddress
            comment = cmd1.ExecuteNonQuery()
 
        ElseIf CheckBox2.Checked Then
            Dim cmd2 As New SqlCommand("sp_emp", con)
            cmd2.CommandType = CommandType.StoredProcedure
            chkboxemp.Value = CheckBox2.Text.ToString()
            cmd2.Parameters.Add("@name", SqlDbType.NVarChar).Value = name
            cmd2.Parameters.Add("@emp", SqlDbType.NVarChar).Value = chkboxemp
            cmd2.Parameters.Add("@postdate", SqlDbType.NVarChar).Value = postdate
            cmd2.Parameters.Add("@ipaddress", SqlDbType.NVarChar).Value = ipaddress
            comment = cmd2.ExecuteNonQuery()
        ElseIf CheckBox3.Checked Then
            Dim cmd3 As New SqlCommand("sp_emp", con)
            cmd3.CommandType = CommandType.StoredProcedure
            chkboxemp.Value = CheckBox3.Text.ToString()
            cmd3.Parameters.Add("@name", SqlDbType.NVarChar).Value = name
            cmd3.Parameters.Add("@emp", SqlDbType.NVarChar).Value = chkboxemp
            cmd3.Parameters.Add("@postdate", SqlDbType.NVarChar).Value = postdate
            cmd3.Parameters.Add("@ipaddress", SqlDbType.NVarChar).Value = ipaddress
            comment = cmd3.ExecuteNonQuery()
 
        End If
        If comment = 1 Then
            Label1.Text = "Your Value is Added"
        Else
            Label1.Text = "Already Exists"
        End If
        
        con.Close()

Open in new window

0
 
Rajar AhmedConsultantAuthor Commented:
nope , not like that ,

there are 3 checkbox  right
employee,manager,ceo

If employee and manager are selected i need to store both in the chkboxemp variable and
so that in my db field type will have both employee,manager ..
For eg
table1 It should be stored like this ::
name      type                               postdate          ipaddress
Michael    employee,manager           10/21/2008     some ip addres
Roger       employee,manager,ceo    1/9/2008          some ip addres
Neoree     employee                         2/21/2008     some ip addres

am i clear..?


0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Pratima PharandeCommented:
then its simple

Dim str as string
str = ""
If CheckBox1.Checked Then
 
  str =   CheckBox1.Text.ToString()
ElseIf CheckBox2.Checked Then
                   if ( str = "") then
                         str =   CheckBox2.Text.ToString()
                    else
         str =   str & "," & CheckBox2.Text.ToString()

                      END if      
 ElseIf CheckBox3.Checked Then
                    if ( str = "") then
                         str =   CheckBox3.Text.ToString()
                    else
         str =   str & "," & CheckBox3.Text.ToString()

                      END if    
        End If
chkboxemp.Value = str
0
 
Rajar AhmedConsultantAuthor Commented:
this is how it luks now ....
this stores only for one value even if three checkbox r selected.....

eventthough i selected employee n manager it stored only employee

plz suggest....




Dim con As SqlConnection
        Dim constr As String = ConfigurationManager.AppSettings("conxn")
        con = New SqlConnection(constr)
        con.Open()
        Dim cmd1 As New SqlCommand("sp_emp5", con)
        cmd1.CommandType = CommandType.StoredProcedure
        Dim name As New SqlParameter("@cname", SqlDbType.NVarChar, 100)
        name.Value = TextBox2.Text()
        Dim chkboxemp As New SqlParameter("@batches1", SqlDbType.NVarChar, 300)
        Dim str As String
        str = ""
        If CheckBox1.Checked Then
            str = CheckBox1.Text.ToString()
        ElseIf CheckBox2.Checked Then
            If (str = "") Then
                str = CheckBox2.Text.ToString()
            Else
                str = str & "," & CheckBox2.Text.ToString()
            End If
        ElseIf CheckBox3.Checked Then
            If (str = "") Then
                str = CheckBox3.Text.ToString()
            Else
                str = str & "," & CheckBox3.Text.ToString()
 
            End If
        End If
        chkboxemp.Value = str
        cmd1.Parameters.Add(name)
        cmd1.Parameters.Add(chkboxemp)
        Dim comment As Integer = cmd1.ExecuteNonQuery()
        If comment = 1 Then
            Label1.Text = "Your Value is Added"
        Else
            Label1.Text = "Already Exists"
        End If
        con.Close()
        

Open in new window

0
 
Rajar AhmedConsultantAuthor Commented:
plz suggest this issue..?

Dim postdate As DateTime = DateTime.Now.ToString()
 Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")

gettin this error..
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.

  this worked ....
If CheckBox1.Checked Then
            str = CheckBox1.Text.ToString()
        End If
        If CheckBox2.Checked Then
            If (str = "") Then
                str = CheckBox2.Text.ToString()
            Else
                str = str & "," & CheckBox2.Text.ToString()
            End If
        End If
        If CheckBox3.Checked Then
            If (str = "") Then
                str = CheckBox3.Text.ToString()
            Else
                str = str & "," & CheckBox3.Text.ToString()
 
            End If
        End If

Open in new window

0
 
Pratima PharandeCommented:
Check like thus  
put some dummy    ipaddress = "127.0.0.0"

 Dim postdate As DateTime = DateTime.Now.ToString()
 Dim ipaddress As String = Request.ServerVariables("REMOTE_HOST")
If (ipaddress = "") Then
         ipaddress = "127.0.0.0"

        End If
0
 
Rajar AhmedConsultantAuthor Commented:
thanks ..
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now