Results 1 to 3 of 3

Thread: [RESOLVED] Updating SQL Database Table

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Resolved [RESOLVED] Updating SQL Database Table

    I am trying to update the password field in a table called tb_Users for a given username. The following code works fine when I use literal values for the new password and for the Username.

    Imports System.Data.SqlClient
    Public Class PasswordForm
    Private Sub ContinueButton_Click(sender As System.Object, e As System.EventArgs) Handles ContinueButton.Click
    If TextBox1.Text = TextBox2.Text Then
    Me.Label4.Visible = False
    Dim SQLStr As String
    Dim ConnString As String
    ConnString = "Data Source=mikespc\sqlexpress; Initial Catalog=db_PigginGifts; Integrated Security=True"
    'SQL Staments
    'SQL query = myQuery = "SQL Statment"
    SQLStr = "UPDATE tb_Users SET Password = 'whogoesthere' Where Username = 'Joe Blogs'"
    'Write to SQL
    Dim SQLConn As New SqlConnection() 'The SQL Connection
    Dim SQLCmd As New SqlCommand() 'The SQL Command
    SQLConn.ConnectionString = ConnString 'Set the Connection String
    SQLConn.Open() 'Open the connection
    SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
    SQLCmd.CommandText = SQLStr 'Sets the SQL String
    SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
    SQLConn.Close() 'Close the connection
    MsgBox(SQLStr)
    Else
    Me.Label4.Visible = True
    End If
    End Sub


    How do I substitute the characters 'whogoesthere' for the contents of a TextBox called TextBox1 and the characters 'Joe Blogs' with the contents of a public varialble called PubUsers ?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,882

    Re: Updating SQL Database Table

    one word: Parameters

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,415

    Re: Updating SQL Database Table

    Also password is a reserved word and should nkt be used as a field name. If it can not be changed then it must be enclosed in square brackets
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width