Solved

Is this the right way to do it? Saving data....

Posted on 2004-10-17
1
134 Views
Last Modified: 2010-04-23
The below code is what I'm using to write to the database. It works but I'm wondering whether it's the best way of doing it. Any comments?

***********************


    Private Sub StartSavingReferralServiceProvider()
        If Not ValidateReferralServiceProviderFields() Then Exit Sub

        'Check the status of the connection
        CheckConnectionStatus()

        Dim cmd As SqlCommand = cnn.CreateCommand

        Try
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "procInsertReferralServiceProvider"

            cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.Text, 100))
            cmd.Parameters("@Name").Value = txt_Name.Text
            cmd.Parameters.Add(New SqlParameter("@Address", SqlDbType.Text, 35))
            cmd.Parameters("@Address").Value = txt_Address.Text
            cmd.Parameters.Add(New SqlParameter("@City", SqlDbType.Text, 35))
            cmd.Parameters("@City").Value = txt_City.Text
            cmd.Parameters.Add(New SqlParameter("@Phone", SqlDbType.Text, 30))
            cmd.Parameters("@Phone").Value = txt_Phone.Text
            cmd.Parameters.Add(New SqlParameter("@Email", SqlDbType.Text, 60))
            cmd.Parameters("@Email").Value = txt_Email.Text
            cmd.Parameters.Add(New SqlParameter("@ContactPerson", SqlDbType.Text, 35))
            cmd.Parameters("@ContactPerson").Value = txt_ContactPerson.Text
            cmd.Parameters.Add(New SqlParameter("@Notes", SqlDbType.Text, 6000))
            cmd.Parameters("@Notes").Value = txt_OrganisationNotes.Text
            cmd.Parameters.Add(New SqlParameter("@UserCreated", SqlDbType.Text, 35))
            cmd.Parameters("@UserCreated").Value = g_Username
            cmd.Parameters.Add(New SqlParameter("@BranchID", SqlDbType.Int))
            cmd.Parameters("@BranchID").Value = g_UserCentre
            cmd.Parameters.Add(New SqlParameter("@ReferralServiceProviderIDAuto", SqlDbType.Int))
            cmd.Parameters("@ReferralServiceProviderIDAuto").Value = m_ReferralServiceProviderIDAuto
            cmd.Parameters.Add(New SqlParameter("@DateCreated", SqlDbType.DateTime, 40))
            cmd.Parameters("@DateCreated").Value = New Date(Today.Year, Today.Month, Today.Day)

            cmd.ExecuteNonQuery()

            If m_ReferralServiceProviderIDAuto = 0 Then
                MessageBox.Show("New Referral Service Provider Successfully Saved!", "Save Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Referral Service Provider Successfully Updated!", "Save Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch e As Exception
            MessageBox.Show("An exception of type " & e.GetType().ToString() & _
            " was encountered while inserting the data. Record failed writing to the database.", "Database Transaction Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            cnn.Close()
        End Try

        txtSearch.Focus()
        GroupBox_Details.Enabled = False
        ToolBar1.Buttons(2).Enabled = False

    End Sub

******************


CREATE PROCEDURE procInsertReferralServiceProvider
      @Name varchar(100),
      @Address varchar(35),
      @City varchar(35),
      @Phone varchar(30),
      @EMail varchar(60),
      @ContactPerson varchar(35),
      @Notes varchar(6000),
      @UserCreated varchar(35),
      @BranchID int,
      @ReferralServiceProviderIDAuto int,
      @DateCreated datetime
AS
      SELECT @Name = REPLACE(@Name,"'","''")
      SELECT @Address = REPLACE(@Address,"'","''")
      SELECT @City = REPLACE(@City,"'","''")
      SELECT @Email = REPLACE(@Email,"'","''")
      SELECT @ContactPerson = REPLACE(@ContactPerson,"'","''")
      SELECT @Notes = REPLACE(@Notes,"'","''")
      
      If @ReferralServiceProviderIDAuto = 0
      BEGIN
            Insert into ReferralServiceProvider (UserCreated,  DateCreated, BranchID, ProviderName, Address, City, Phone, Email, ContactPerson, Description)
            Values (@UserCreated, @DateCreated,  @BranchID, @Name, @Address, @City, @Phone, @Email, @ContactPerson, @Notes)
      END
      Else
            Update ReferralServiceProvider
            Set UserCreated = @UserCreated,  BranchID = @BranchID, ProviderName = @Name, Address = @Address,
            City = @City, Phone = @Phone, Email = @Email, ContactPerson = @ContactPerson, Description = @Notes
            where ReferralServiceProviderIDAuto = @ReferralServiceProviderIDAuto

GO
0
Comment
Question by:PeterErhard
1 Comment
 
LVL 10

Accepted Solution

by:
123654789987 earned 250 total points
ID: 12336259
I have only one comment. Instead of doing this in the stored procedure

AS
     SELECT @Name = REPLACE(@Name,"'","''")


u can apply the replace to textbox text

 cmd.Parameters("@Name").Value = txt_Name.Text.Replace("'","''")




0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

15 Experts available now in Live!

Get 1:1 Help Now