Solved

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

Posted on 2004-10-17
1
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Ctime to date time in textfile? 7 81
MYSQL responding very slow 3 65
UDP Data Capture Thread Issue 7 41
How to call new form in VB2013? 4 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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