Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:18:32+00:00 2026-05-30T15:18:32+00:00

I have managed to create a connection to the database and am able to

  • 0

I have managed to create a connection to the database and am able to save information to it from my form, the form contains 22 textboxes, a save and another exit button.

I have set the form to retrieve data in the form_load. I have used the “UPDATE” SQL command on the save button and it does save the data on all 22 textboxes (all textboxes are linked to their separate columns). But only one record will be needed that is why I did not use the “INSERT” command.

The problem arises when I click the save button, all (19) textboxes are saved but when I retrieve the text (by reloading the form), each of the 20, 21, 22 textboxes (only) the text of the 20th textbox keeps on moving tho the next textbox e.g. 20>21>22>20>21>22 is interchanged amongst themselves.

Please help me (I googled this but found nothing on this) and please tell me why this is happening.

The code is below:

Dim sqCon As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;Database=MegaDatabase;Integrated Security=SSPI")
    Dim sqCmd As New SqlClient.SqlCommand

    sqCmd.Connection = sqCon

    Dim DatabaseErrorMsg As String = "Unable to save the details. Please contact the program developers."
    Dim DatabaseErrorTitle As String = "Database Editing Error"
    Dim DatabaseDoneSave As String = "New records made/updated successfully"

    Dim CompNmtxtS As String
    Dim TrdNmtxtS As String
    Dim ComRegtxtS As String
    Dim WTNmtxtS As String
    Dim VRegtxtS As String
    Dim TextBox1S As String
    Dim ComPosttxtS As String
    Dim StrAddrtxtS As String
    Dim ComCitytxtS As String
    Dim ComCounttxtS As String
    Dim RegAddrtxtS As String
    Dim ComZiptxtS As String
    Dim RepTeltxtS As String
    Dim ComFaxtxtS As String
    Dim RepCelltxtS As String
    Dim W_URLtxtS As String
    Dim EWebtxtS As String
    Dim BankNametxtS As String
    Dim BankBranchtxtS As String
    Dim BraCodetxtS As String
    Dim BankAcctxtS As String
    Dim TextBox2S As String
    CompNmtxtS = CompNmtxt.Text
    TrdNmtxtS = TrdNmtxt.Text
    ComRegtxtS = ComRegtxt.Text
    WTNmtxtS = WTNmtxt.Text
    VRegtxtS = VRegtxt.Text
    TextBox1S = TextBox1.Text
    ComPosttxtS = ComPosttxt.Text
    StrAddrtxtS = StrAddrtxt.Text
    ComCitytxtS = ComCitytxt.Text
    ComCounttxtS = ComCounttxt.Text
    RegAddrtxtS = RegAddrtxt.Text
    ComZiptxtS = ComZiptxt.Text
    RepTeltxtS = RepTeltxt.Text
    ComFaxtxtS = ComFaxtxt.Text
    RepCelltxtS = RepCelltxt.Text
    ComFaxtxtS = ComFaxtxt.Text
    W_URLtxtS = W_URLtxt.Text
    EWebtxtS = EWebtxt.Text
    BankNametxtS = BankNametxt.Text
    BankBranchtxtS = BankBranchtxt.Text
    BraCodetxtS = BraCodetxt.Text
    BankAcctxtS = BankAcctxt.Text
    TextBox2S = TextBox2.Text
    Try

        '*NOTE: UPDATE function will only UPDATE the fields when there is already something in there, as it cannot work for the INSERT command
    'Format for UPDATE command: USE DatabaseName; UPDATE Tablename SET ColumnName = '" & declared string name & "'"
        sqCmd.CommandText = ("USE MegaDatabase; UPDATE CompDetails SET CompName = '" & CompNmtxtS & "', TradeName = '" & TrdNmtxtS & "', CompReg = '" & ComRegtxtS & "', WTnum = '" & WTNmtxtS & "', VATregNo = '" & VRegtxtS & "', TaxPeriod = '" & TextBox1S & "', CompPostalAddr = '" & ComPosttxtS & "', CompPhysAddr = '" & StrAddrtxtS & "', CompCity = '" & ComCitytxtS & "', CompCountry = '" & ComCounttxtS & "', CompProvince = '" & RegAddrtxtS & "', CompZip = '" & ComZiptxtS & "', CompTel = '" & RepTeltxtS & "', CompFax = '" & ComFaxtxtS & "', CompCell = '" & RepCelltxtS & "', CompWebsite = '" & W_URLtxtS & "', CompEmail = '" & EWebtxtS & "', CompBankName = '" & BankNametxtS & "', CompBranchName = '" & BankBranchtxtS & "', CurrentTaxTable = '" & TextBox2S & "', CompBranchCode = '" & BraCodetxtS & "', CompAccNo = '" & BankAcctxtS & "'")

    sqCon.Open()
    sqCmd.ExecuteNonQuery()
        sqCon.Close()
    Catch ex As Exception
        MessageBox.Show(DatabaseErrorMsg, DatabaseErrorTitle, MessageBoxButtons.OK)
    Finally
        Me.Close()
        frmMDImainform.MainMenuStrip.Enabled = True
        MessageBox.Show(DatabaseDoneSave, "Done Saving...", MessageBoxButtons.OK)
    End Try

For the loading part:

     Try
        sqCmd.CommandText = "SELECT TOP 1 [CompName],[TradeName],[CompReg],[WTnum],[VATregNo],[TaxPeriod],[CompPostalAddr],[CompPhysAddr],[CompCity],[CompCountry],[CompProvince],[CompZip],[CompTel],[CompFax],[CompCell],[CompWebsite],[CompEmail],[CompBankName],[CompBranchName],[CurrentTaxTable],[CompBranchCode],[CompAccNo] FROM [MegaDatabase].[dbo].[CompDetails]"
        sqCon.Open()
        sqRdr = sqCmd.ExecuteReader()
    Catch ex As Exception

    End Try



Do While sqRdr.Read() 'No need for VbTab and Vb crlf
            CompNmtxt.Text = CompNmtxt.Text & sqRdr.GetValue(0)
            TrdNmtxt.Text = TrdNmtxt.Text & sqRdr.GetValue(1)
            ComRegtxt.Text = ComRegtxt.Text & sqRdr.GetValue(2)
            WTNmtxt.Text = WTNmtxt.Text & sqRdr.GetValue(3)
            VRegtxt.Text = VRegtxt.Text & sqRdr.GetValue(4)
            TextBox1.Text = TextBox1.Text & sqRdr.GetValue(5)
            ComPosttxt.Text = ComPosttxt.Text & sqRdr.GetValue(6)
            StrAddrtxt.Text = StrAddrtxt.Text & sqRdr.GetValue(7)
            ComCitytxt.Text = ComCitytxt.Text & sqRdr.GetValue(8)
            ComCounttxt.Text = ComCounttxt.Text & sqRdr.GetValue(9)
            RegAddrtxt.Text = RegAddrtxt.Text & sqRdr.GetValue(10)
            ComZiptxt.Text = ComZiptxt.Text & sqRdr.GetValue(11)
            RepTeltxt.Text = RepTeltxt.Text & sqRdr.GetValue(12)
            ComFaxtxt.Text = ComFaxtxt.Text & sqRdr.GetValue(13)
            RepCelltxt.Text = RepCelltxt.Text & sqRdr.GetValue(14)
            W_URLtxt.Text = W_URLtxt.Text & sqRdr.GetValue(15)
            EWebtxt.Text = EWebtxt.Text & sqRdr.GetValue(16)
            BankNametxt.Text = BankNametxt.Text & sqRdr.GetValue(17)
            BankBranchtxt.Text = BankBranchtxt.Text & sqRdr.GetValue(18)
            BraCodetxt.Text = BraCodetxt.Text & sqRdr.GetValue(19)
            BankAcctxt.Text = BankAcctxt.Text & sqRdr.GetValue(20)
            TextBox2.Text = TextBox2.Text & sqRdr.GetValue(21)
        Loop

Thanks for reading

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:18:33+00:00Added an answer on May 30, 2026 at 3:18 pm

    You have a mixup in the order of your columns in your query vs. accessing the columns by index when reading your resultset:

    In your query column #21 (last column, zero-based index) is [CompAccNo] but when reading you assign column #21 to TextBox2.Text.

    I’d suggest you rather access your columns by name when reading:

    CompNmtxt.Text = CompNmtxt.Text & sqRdr.GetValue("CompName")
    TrdNmtxt.Text = TrdNmtxt.Text & sqRdr.GetValue("TradeName")
    ComRegtxt.Text = ComRegtxt.Text & sqRdr.GetValue("CompReg")
    '...
    

    Another point: Get rid of creating your update query by concatenating user input – use Command Parameters instead!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've deconstructed a standard WPF button using Blend and have managed to create a
I have managed to create a custom action in C# using MakeSfxCA which is
I have tried FBConnect for iPhone and am able to create/manage sessions and upload
In our web project, we have a custom Database Connection class that creates the
Following this tutorial http://netbeans.org/kb/docs/javaee/maven-osgiservice-cdi.html I have managed to create a simple OSGI bundle and
I have a page which basically allows an admin user to create manager user
I have an Application (an appointment manager) which lets the user create usercontrols in
Suppose I have a stored procedure that manages its own transaction CREATE PROCEDURE theProc
I'm having a problem with mixing managed and unmanaged code. I have created two
At work we currently have a very large web application with a connection to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.