Solved

How do I get data from checkboxlist into a gridview?

Posted on 2014-04-19
6
308 Views
Last Modified: 2014-04-20
Checkboxlist help? When I submit my data I get this error:

System.Web.UI.WebControls.ListItemCollection      

Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Data.OleDb
Imports System.DateTime

Partial Class Submit
    Inherits System.Web.UI.Page
    Dim objdt As System.Data.DataTable
    Dim objdr As System.Data.DataRow
    Dim allthemes As String = ""
    Dim ds As New DataSet
    Dim i As Integer
    Dim strtheme As String
    Dim savenow As DateTime = DateTime.Now





    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim strConnection As String = "Data Source=mssql102.manage.myhosting.com;Initial Catalog=db1150602_group3;Integrated Security=False;User ID=u1150602_drnicholsongroup3;Password=w6p?K9x!,l;Connect Timeout=15;Encrypt=False;Packet Size=4096"
        Dim objconnection As New SqlConnection(strConnection)
        Dim mysqlDataAdapter As New SqlDataAdapter("select * From Publication", objconnection)
        'Using conn As New SqlConnection(strConnection)
        'Using()  cmd As New SqlCommand(selecteditems, conn)
        'cmd.Parameters.AddWithValue("@CheckBoxList1", CheckBoxList1.Items)

        'conn.Open()

        'selecteditems = DirectCast(cmd.ExecuteScalar(), String)
        'End Using
        'End Using
        Dim myDataRow As DataRow
        Dim mydatarowscommandbuilder As New SqlCommandBuilder(mysqlDataAdapter)


        mysqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        mysqlDataAdapter.Fill(ds, "Publication")

        myDataRow = ds.Tables("Publication").NewRow()
        myDataRow("PubTypeID") = DropDownList1.SelectedItem
        myDataRow("Addinfo") = txtAddInfo.Text
        myDataRow("coAuthor") = txtCoAuthor.Text
        myDataRow("coAuthor2") = txtCoAuthor2.Text
        myDataRow("coAuthor3") = txtCoAuthor3.Text
        myDataRow("coAuthor4") = txtCoAuthor4.Text
        myDataRow("coAuthor5") = txtCoAuthor5.Text
        myDataRow("Themes") = CheckBoxList1.Items
        myDataRow("PubDate") = DateTime.Now
        myDataRow("WorkTitle") = txtWorkTitle.Text
        myDataRow("AuthorFName") = txtFirstName.Text
        myDataRow("AuthorLName") = txtLastName.Text
        myDataRow("AuthorEmail") = txtEmail.Text
        ds.Tables("Publication").Rows.Add(myDataRow)
        myDataRow("FacultyID") = Session("user")
        'myDataRow("EventID") = ?
        mysqlDataAdapter.Update(ds, "Publication")

        txtAddInfo.Text = ""
        txtCoAuthor.Text = ""
        txtCoAuthor2.Text = ""
        txtCoAuthor3.Text = ""
        txtCoAuthor4.Text = ""
        txtCoAuthor5.Text = ""
        txtWorkTitle.Text = ""
        txtFirstName.Text = ""
        txtLastName.Text = ""
        txtEmail.Text = ""


        Try
            Dim Smtp_Server As New SmtpClient
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("username@rowan.edu", "password")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            Smtp_Server.Host = "smtp.gmail.com"
            e_mail = New MailMessage()
            e_mail.From = New MailAddress("username@rowan.edu")
            e_mail.To.Add("username@rowan.edu")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = txtFirstName.Text
            e_mail.Body = txtLastName.Text
            e_mail.Body = DropDownList1.DataTextField
            e_mail.Body = txtWorkTitle.Text
            e_mail.Body = txtCoAuthor.Text
            e_mail.Body = txtCoAuthor2.Text
            e_mail.Body = txtCoAuthor3.Text
            e_mail.Body = txtCoAuthor4.Text
            e_mail.Body = txtCoAuthor5.Text

            e_mail.Body = txtAddInfo.Text
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent") '
        Catch error_t As Exception
            MsgBox(error_t.ToString)
        End Try


    End Sub




    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnAdd.Click

        If (txtCoAuthor2.Visible = False) Then

            txtCoAuthor2.Visible = True
            Return

        End If

        If (txtCoAuthor3.Visible = False) Then



            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            Return
        End If


        If (txtCoAuthor4.Visible = False) Then



            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            txtCoAuthor4.Visible = True
            Return
        End If

        If (txtCoAuthor5.Visible = False) Then

            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            txtCoAuthor4.Visible = True
            txtCoAuthor5.Visible = True
            Return
        End If

        getThemes()

    End Sub

    Public Sub getThemes()
        Dim i As Integer
        Dim chkbx As CheckBoxList
        chkbx = CType(FormView1.FindControl("CheckBoxList1"), CheckBoxList)
        Dim sb As StringBuilder = New StringBuilder()
        For i = 0 To chkbx.Items.Count - 1
            If chkbx.Items(i).Selected Then
                sb.Append(chkbx.Items(i).Value & ",")
            End If
        Next
        'Create the value to be inserted by removing the last comma in sb
        Dim InputValue As String
        InputValue = Left(sb.ToString(), Len(sb.ToString()) - 1)

    End Sub

End Class

Open in new window

0
Comment
Question by:ryanbecker24
  • 3
  • 3
6 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 40011849
do you get the error on
myDataRow("PubTypeID") = DropDownList1.SelectedItem

because it is possible that the SelectedItem is an object, without the expected string representation ?   you may need to refer to the desired value of object
0
 

Author Comment

by:ryanbecker24
ID: 40011852
I updated my code with other bits, so here it is. I don't get an error there and my information for that row works perfectly.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Data.OleDb
Imports System.DateTime

Partial Class Submit
    Inherits System.Web.UI.Page
    Dim objdt As System.Data.DataTable
    Dim objdr As System.Data.DataRow
    Dim allthemes As String = ""
    Dim ds As New DataSet
    Dim ds2 As New DataSet
    Dim i As Integer
    Dim strtheme As String
    Dim savenow As DateTime = DateTime.Now





    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim strConnection As String = "Data Source=mssql102.manage.myhosting.com;Initial Catalog=db1150602_group3;Integrated Security=False;User ID=u1150602_drnicholsongroup3;Password=w6p?K9x!,l;Connect Timeout=15;Encrypt=False;Packet Size=4096"
        Dim objconnection As New SqlConnection(strConnection)
        Dim objconnection2 As New SqlConnection(strConnection)
        Dim mysqlDataAdapter As New SqlDataAdapter("select * From Publication", objconnection)
        Dim mysqlDataAdapter2 As New SqlDataAdapter("select * From Event", objconnection2)
        'Using conn As New SqlConnection(strConnection)
        'Using()  cmd As New SqlCommand(selecteditems, conn)
        'cmd.Parameters.AddWithValue("@CheckBoxList1", CheckBoxList1.Items)

        'conn.Open()

        'selecteditems = DirectCast(cmd.ExecuteScalar(), String)
        'End Using
        'End Using
        Dim myDataRow As DataRow
        Dim mydatarowscommandbuilder As New SqlCommandBuilder(mysqlDataAdapter)
        Dim mydatarowscommandbuilder2 As New SqlCommandBuilder(mysqlDataAdapter2)

        mysqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        mysqlDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey
        mysqlDataAdapter.Fill(ds, "Publication")
        mysqlDataAdapter2.Fill(ds, "Event")
        myDataRow = ds.Tables("Publication").NewRow()
        myDataRow("PubTypeID") = DropDownList1.SelectedItem
        myDataRow("Addinfo") = txtAddInfo.Text
        myDataRow("coAuthor") = txtCoAuthor.Text
        myDataRow("coAuthor2") = txtCoAuthor2.Text
        myDataRow("coAuthor3") = txtCoAuthor3.Text
        myDataRow("coAuthor4") = txtCoAuthor4.Text
        myDataRow("coAuthor5") = txtCoAuthor5.Text
        myDataRow("Themes") = CheckBoxList1.Items
        myDataRow("PubDate") = DateTime.Now
        myDataRow("WorkTitle") = txtWorkTitle.Text
        myDataRow("AuthorFName") = txtFirstName.Text
        myDataRow("AuthorLName") = txtLastName.Text
        myDataRow("AuthorEmail") = txtEmail.Text
        ds.Tables("Publication").Rows.Add(myDataRow)
        myDataRow("FacultyID") = Session("user")
        myDataRow("EventID") = "Scholarfest 2014"
        mysqlDataAdapter.Update(ds, "Publication")
        mysqlDataAdapter2.Update(ds, "Event")

        txtAddInfo.Text = ""
        txtCoAuthor.Text = ""
        txtCoAuthor2.Text = ""
        txtCoAuthor3.Text = ""
        txtCoAuthor4.Text = ""
        txtCoAuthor5.Text = ""
        txtWorkTitle.Text = ""
        txtFirstName.Text = ""
        txtLastName.Text = ""
        txtEmail.Text = ""


        Try
            Dim Smtp_Server As New SmtpClient
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("username@rowan.edu", "password")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            Smtp_Server.Host = "smtp.gmail.com"
            e_mail = New MailMessage()
            e_mail.From = New MailAddress("username@rowan.edu")
            e_mail.To.Add("username@rowan.edu")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = txtFirstName.Text
            e_mail.Body = txtLastName.Text
            e_mail.Body = txtEmail.Text
            'e_mail.Body = ToString(CheckBoxList1.Items)
            e_mail.Body = DropDownList1.DataTextField
            e_mail.Body = DateTime.Now
            e_mail.Body = txtWorkTitle.Text
            e_mail.Body = txtCoAuthor.Text
            e_mail.Body = txtCoAuthor2.Text
            e_mail.Body = txtCoAuthor3.Text
            e_mail.Body = txtCoAuthor4.Text
            e_mail.Body = txtCoAuthor5.Text

            e_mail.Body = txtAddInfo.Text
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent") '
        Catch error_t As Exception
            MsgBox(error_t.ToString)
        End Try

        Response.Redirect("thankYouSubmitPub.aspx")
    End Sub




    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnAdd.Click

        If (txtCoAuthor2.Visible = False) Then

            txtCoAuthor2.Visible = True
            Return

        End If

        If (txtCoAuthor3.Visible = False) Then



            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            Return
        End If


        If (txtCoAuthor4.Visible = False) Then



            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            txtCoAuthor4.Visible = True
            Return
        End If

        If (txtCoAuthor5.Visible = False) Then

            txtCoAuthor2.Visible = True
            txtCoAuthor3.Visible = True
            txtCoAuthor4.Visible = True
            txtCoAuthor5.Visible = True
            Return
        End If

        getThemes()
        Response.Redirect("thankYouSubmitPub.aspx")
    End Sub

    Public Sub getThemes()
        Dim i As Integer
        Dim chkbx As CheckBoxList
        chkbx = CType(FormView1.FindControl("CheckBoxList1"), CheckBoxList)
        Dim sb As StringBuilder = New StringBuilder()
        For i = 0 To chkbx.Items.Count - 1
            If chkbx.Items(i).Selected Then
                sb.Append(chkbx.Items(i).Value & ",")
            End If
        Next
        'Create the value to be inserted by removing the last comma in sb
        Dim InputValue As String
        InputValue = Left(sb.ToString(), Len(sb.ToString()) - 1)

    End Sub



End Class

Open in new window

0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 40011858
can you advise where the error throws ?

what data are you trying to read / write at the time ?

its probably this row after looking again.
myDataRow("Themes") = CheckBoxList1.Items

Items is a collection where Themes is a field. What type ?

you wilt need to loop through the items list, building the field data/

'eg
outfield = ""
for each itx in CheckBoxList1.Items
   if itx.Selected then
       outfield = outfield & "," & itx.Value
   end if
next itx
outfield = mid(outfield,2)   'drop the lead comma

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ryanbecker24
ID: 40011905
I don't get any error when I run my code. In my gridview it keeps on saying System.Web.UI.WebControls.ListItemCollection instead of my selected checkboxes. Just tried your code, same result.

I'm building the checkboxes in a list and sending them to the database, so String?
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 40011942
ok no error but is it the "themes" that is showing incorrctly in the grid ?

it is showing the text "System.Web.UI.WebControls.ListItemCollection" because that is the name of the object and its default value.

myDataRow("Themes") = CheckBoxList1.Items   is not correct.  try replacing it with code below..

outfield = ""
dim itx as ListItem
for each itx in CheckBoxList1.Items
   if itx.Selected then
       outfield = outfield & "," & itx.Value
   end if
next itx
outfield = mid(outfield,2)   'drop the lead comma
myDataRow("Themes") = outfield

Open in new window


You have a GetThemes routine in your code but not using it ? not sure that the findcontrol part is necessary.
0
 

Author Comment

by:ryanbecker24
ID: 40011975
Thanks, it works.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now