Help Creating a function to change SQL Field Values

Posted on 2007-11-18
Last Modified: 2011-09-20
Hi all

Im looking for some help in creating a function to allow me to hardcode updates into SQL. The attached code will allow me to open the DB and search the fields however its been a while since ive programmed in vb and am moving to .net so hence the .field("Name") = is a bit out of date..  :)

Any help would be great

Many Thanks

Code in use __________________________________

       Dim SQL As String = "SELECT * FROM USERINFO WHERE USERNAME = '" & txtUsername.Text & "' AND PASSWORD  = '" & CryptPass & "'"

        Dim sqlReader As SqlDataReader = CCDBC.CCDBC.RetrieveData(SQL)

        If sqlReader.Read Then

            With sqlReader

                MsgBox(.GetValue(1) & "   " & .GetValue(2) & "   " & .GetValue(3))

            End With



            MsgBox("Invalid Username or Password entered", MsgBoxStyle.Exclamation)

        End If


        sqlReader = Nothing


Public Shared Function GetConnection() As SqlClient.SqlConnection

        Dim conn As SqlClient.SqlConnection

        Dim strConn As String = "Data Source=SQLEXPRESS;Initial Catalog=CC;Integrated Security=True;Pooling=False"

        conn = New SqlClient.SqlConnection(strConn)



        Catch ex As SqlClient.SqlException

            Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)

        Catch ex As Exception

            Console.WriteLine("ERROR opening connection: " & ex.Message)

        End Try

        GetConnection = conn

    End Function

    Public Shared Function RetrieveData(ByVal query As String) As SqlClient.SqlDataReader

        Dim rdr As SqlClient.SqlDataReader

        Dim conn As SqlClient.SqlConnection = GetConnection()


            Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(query, conn)

            ''CommandBehavior.CloseConnection closes the underlying connection 

            ''whenever the reader is closed

            rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Catch ex As SqlClient.SqlException

            Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)

        Catch ex As Exception

            Console.WriteLine("ERROR retrieving data: " & ex.Message)

        End Try

        RetrieveData = rdr

    End Function

Open in new window

Question by:Stephen Manderson
1 Comment
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 20310503
please check out this tutorial:

it tells you to fill in a sqldataadapter, and define the UpdateCommand that will submit the sql update (or procedure) to save any changes done to the data inthe sqldataadapter...

hope this clarifies how to do things the .net way

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

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

20 Experts available now in Live!

Get 1:1 Help Now