Solved

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

Posted on 2004-10-17
1
141 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

Technology Partners: 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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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