Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Failed to update and save Checkbox controls action to a field of SQL database table

Posted on 2012-12-30
4
Medium Priority
?
425 Views
Last Modified: 2013-01-02
Hi Gurus,

my web app page has 3 checkbox and 1 text field, I need to submit values of these controls by the button control, and update a field in the SQL Server 2005 database, by matching a field called [File Number] .

I put my code below, no error when debugging, but no data saved in the data table as well.

Please advise.


 Protected Sub Button1AfterReadLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1AfterReadLetter.Click


        Dim Username2 As String = Request.QueryString("Val")


        Dim DBconn As New SqlClient.SqlConnection("Data Source=Myserver;Initial Catalog=MyDB;Integrated Security=True;")
        Dim DBcmd As New SqlClient.SqlCommand
        Dim DBAdap As New SqlClient.SqlDataAdapter
        Dim DS As New DataSet

        DBconn.Open()
        Try
            DBcmd = New SqlClient.SqlCommand("update MyTable set EnglishCheckbox =@eCheckbox where [File Number] = @File Number", DBconn)
            DBcmd = New SqlClient.SqlCommand("update MyTable  set SpanishCheckbox=@sCheckbox where [File Number] = @File Number", DBconn)
            DBcmd = New SqlClient.SqlCommand("update  MyTable  set OtherLanguageCBox=@oCheckbox where [File Number] = @File Number", DBconn)
            DBcmd = New SqlClient.SqlCommand("update MyTable set languageName=@LanguageName where [File Number] = @File Number", DBconn)

            DBcmd.Parameters.Add("@eCheckbox", SqlDbType.Char).Value = CheckBox1.Checked
            DBcmd.Parameters.Add("@sCheckbox", SqlDbType.Char).Value = CheckBox2.Checked
            DBcmd.Parameters.Add("@oCheckbox", SqlDbType.Char).Value = CheckBox3.Checked
            DBcmd.Parameters.Add("@LanguageName", SqlDbType.VarChar).Value = TextBox1.Text

        Catch exp As Exception
            Response.Write(exp)
        End Try
        DBcmd.Dispose()
        DBconn.Close()
        DBconn = Nothing

        Response.Redirect("WebForm2.aspx?val2=" + Username2)

    End Sub
0
Comment
Question by:GaiaGia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:chwong67
ID: 38731867
Your dmcmd must split. latest string will overwrite all.


Protected Sub Button1AfterReadLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1AfterReadLetter.Click


        Dim Username2 As String = Request.QueryString("Val")


        Dim DBconn As New SqlClient.SqlConnection("Data Source=Myserver;Initial Catalog=MyDB;Integrated Security=True;")
        Dim DBcmd As New SqlClient.SqlCommand
        Dim DBcmd1 As New SqlClient.SqlCommand
        Dim DBcmd2 As New SqlClient.SqlCommand
        Dim DBcmd3 As New SqlClient.SqlCommand
        Dim DBAdap As New SqlClient.SqlDataAdapter
        Dim DS As New DataSet

        DBconn.Open()
        Try
            DBcmd1 = New SqlClient.SqlCommand("update MyTable set EnglishCheckbox =@eCheckbox where [File Number] = @File Number", DBconn)
            DBcmd2 = New SqlClient.SqlCommand("update MyTable  set SpanishCheckbox=@sCheckbox where [File Number] = @File Number", DBconn)
            DBcmd3 = New SqlClient.SqlCommand("update  MyTable  set OtherLanguageCBox=@oCheckbox where [File Number] = @File Number", DBconn)
            DBcmd = New SqlClient.SqlCommand("update MyTable set languageName=@LanguageName where [File Number] = @File Number", DBconn)

            DBcmd1.Parameters.Add("@eCheckbox", SqlDbType.Char).Value = CheckBox1.Checked
            DBcmd2.Parameters.Add("@sCheckbox", SqlDbType.Char).Value = CheckBox2.Checked
            DBcmd3.Parameters.Add("@oCheckbox", SqlDbType.Char).Value = CheckBox3.Checked
            DBcmd.Parameters.Add("@LanguageName", SqlDbType.VarChar).Value = TextBox1.Text

        Catch exp As Exception
            Response.Write(exp)
        End Try
        DBcmd.Dispose()
        DBconn.Close()
        DBconn = Nothing

        Response.Redirect("WebForm2.aspx?val2=" + Username2)

    End Sub 

Open in new window

0
 
LVL 9

Accepted Solution

by:
chwong67 earned 1520 total points
ID: 38731870
improved code:
    Protected Sub Button1AfterReadLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1AfterReadLetter.Click


        Dim Username2 As String = Request.QueryString("Val")


        Dim DBconn As New SqlClient.SqlConnection("Data Source=Myserver;Initial Catalog=MyDB;Integrated Security=True;")
        Dim DBcmd As New SqlClient.SqlCommand
        Dim DBAdap As New SqlClient.SqlDataAdapter
        Dim DS As New DataSet

        DBconn.Open()
        Try
            DBcmd = New SqlClient.SqlCommand("update MyTable set EnglishCheckbox =@eCheckbox, SpanishCheckbox=@sCheckbox, OtherLanguageCBox=@oCheckbox, languageName=@LanguageName where [File Number] = @File Number", DBconn)
            DBcmd.Parameters.Add("@eCheckbox", SqlDbType.Char).Value = CheckBox1.Checked
            DBcmd.Parameters.Add("@sCheckbox", SqlDbType.Char).Value = CheckBox2.Checked
            DBcmd.Parameters.Add("@oCheckbox", SqlDbType.Char).Value = CheckBox3.Checked
            DBcmd.Parameters.Add("@LanguageName", SqlDbType.VarChar).Value = TextBox1.Text
DBcmd.ExecuteNonQuery()
        Catch exp As Exception
            Response.Write(exp)
        End Try
        DBcmd.Dispose()
        DBconn.Close()
        DBconn = Nothing

        Response.Redirect("WebForm2.aspx?val2=" + Username2)

    End Sub 
                                            

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 480 total points
ID: 38732082
First, the DataAdapter and the DataSet are useless. You do not use them, don't you? You don't need them.

And chwong67 gave you the solution, without explaining. First you do not need a command for each field, you can do everything in only one command.

And there was one very important missing line in your code, line 19 in chwong67 code, the line where he executes the command. Simply creating a command does not execute it.
0
 

Author Comment

by:GaiaGia
ID: 38736285
Thank you very much both for pointing out my problem, I am done now.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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