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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

12 Experts available now in Live!

Get 1:1 Help Now