Results 1 to 8 of 8

Thread: stored procedure to insert new row

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    7

    stored procedure to insert new row

    Hello all,

    I'm new to asp.net and stored procedures and could really use some help and guidance.

    Here is what I'm trying to accomplish:

    I have a request infromation form with several text boxes, a submit button and a stored procedure. I'm using asp.net and ms SQL.

    I want the users to be able to fill out the form and get a thank you message, if they have not already requested information from an earlier date. I thought that I could just check to see if the their supplied email address was already in a row in the database and if it was give the user a message to choose a different email address. If the email was not in the database then give a thank you message.

    Can someone please help me get this off the ground?

    Here is the stored procedure:

    CREATE Procedure sp_requestInfo

    @firstName varchar(50),
    @middleName varchar(50),
    @lastName varchar(50),
    @address varchar(50),
    @city varchar(50),
    @state varchar(50),
    @zipCode varchar(50),
    @country varchar(50),
    @email varchar(50),
    @phone varchar(50),
    @fax varchar(50),
    @id int output
    As
    set nocount on
    if not exists(select id from xxxx.dbo.xxxx where email=@email)
    begin
    INSERT INTO xxxx(firstName,middleName,lastName,address,city,state,zipCode,country,email,phone,fax)
    VALUES (@firstname, @middleName, @lastName, @address, @city,@state, @zipCode, @country, @email, @phone, @fax)
    select @id=@@identity
    end
    else
    select @id=-1
    return
    GO


    Here is the buttons click event:

    Dim myconnection As SqlConnection
    myconnection = New SqlConnection("data source=xxxx; User ID=xxxx; Password=xxxx; Persist Security Info=True;packet size=4096")
    myconnection.Open()

    Dim MyCommandd As SqlCommand
    MyCommandd = New SqlCommand("sprequestInfo", myconnection)
    MyCommandd.CommandType = CommandType.StoredProcedure


    What I get: Nothing! no error, no new data, nothing. Please help.

  2. #2
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: stored procedure to insert new row

    You need to execute the procedure,

    Your procedure is ok, although I would suggest putting in some defaults just in case. You need to Execute the stored procedure, I think it's ExecuteNonQuery() or something to that effect...

    Then you can either set a label to say Thank You for registering,
    or you can auto send an email using System.Web.Mail...or you can use jscript to display a thank you message box.

    Jon

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    7

    It works!

    Well, almost.

    The stored procedure does work and data from the form is populating the database if the email address is not in the database and is not populating the database if the email is already in the database. Oh yeah!

    Now the next task. It's like pulling teeth, I know.

    I want a lable to inform the user if the request was successful or not. For example, if the row was added then write to the lable "thank you". If the row was not added, then write to the lable "please choose another email address".


    This can be done with an if statement?

    If id.Value = -1 Then
    status.Text = "Thank you, your information request packet will shipped as soon as possible."
    Else
    status.Text = "The email address you entered already exists, please use another email address."

    End If



    Something is not right here. If I enter an email that exists, I get the "The email address you entered already exists, please use another email address."

    But, if I change the address and the row is infact populated, I still get the same message, "The email address you entered already exists, please use another email address."


    Can someone please shed light as to why?

  4. #4
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: It works!

    Take a step back...
    learn how to do it properly...

    Your SPROC should return @@Scope_Identity() should the insert succeed. This means it will be non-zero. In fact it will be greater than one.

    So you simply need to return an output parameter and then check that parameter.

    Here is an example

    VB Code:
    1. Function InsertUser() As Boolean
    2.         'function to insert a new user into the database
    3.  
    4.         Dim conMyData As SqlConnection
    5.         Dim cmdSelect As SqlCommand
    6.         Dim reader As SqlDataReader
    7.         Dim parmReturnValue As SqlParameter
    8.         Dim intResult As Integer
    9.         Dim bNew As Boolean
    10.  
    11.         'try and make a connection
    12.         Try
    13.             conMyData = New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
    14.             cmdSelect = New SqlCommand("insert_register_user", conMyData)
    15.  
    16.             With cmdSelect
    17.                 .CommandType = CommandType.StoredProcedure
    18.                 parmReturnValue = .Parameters.Add("RETURN_VALUE", SqlDbType.Int)
    19.                 parmReturnValue.Direction = ParameterDirection.ReturnValue
    20.                 'add the parameters
    21.                 .Parameters.Add("@FirstName", txtFirstName.Text)
    22.                 .Parameters.Add("@LastName", txtLastName.Text)
    23.                 .Parameters.Add("@Login", LCase(txtUserName.Text))
    24.                 .Parameters.Add("@ClientID", ddlClient.SelectedItem.Value)
    25.                 .Parameters.Add("@EmailAddress", txtEmail.Text)
    26.                 .Parameters.Add("@Password", txtPassword.Text)
    27.                 .Parameters.Add("@Title", txtTitle.Text)
    28.                 .Parameters.Add("@Telephone", txtTelephone.Text)
    29.                 conMyData.Open()
    30.                 .ExecuteNonQuery()
    31.                 intResult = .Parameters("RETURN_VALUE").Value
    32.             End With
    33.  
    34.             'check if it was a good insert
    35.             If intResult = -1 Then
    36.                 bNew = False
    37.             Else
    38.                 'do nothing
    39.                 bNew = True
    40.             End If
    41.         Catch e As Exception
    42.             Response.Write("An Error Occurred: " & e.ToString())
    43.             'clean up and close resources
    44.         Finally
    45.             cmdSelect = Nothing
    46.             conMyData.Close()
    47.             conMyData = Nothing
    48.         End Try
    49.  
    50.         InsertUser = bNew
    51.     End Function

    Then you can do:

    VB Code:
    1. If InsertUser() Then
    2.                     'then do the email send
    3.                     SendConfirmation()
    4.                     lblMessage.ForeColor = Color.Green
    5.                     'finally give the user a message
    6.                     lblMessage.Text = "Thank You for registering as an IMS User." & vbCrLf & "Your username to enter the system is: " & LCase(Me.txtUserName.Text) & "." & vbCrLf & "An e-mail message will be sent to you with login details."
    7.                     hlLogin.Text = "Go back to the IMS Login page."
    8.                     hlLogin.NavigateUrl = "login.aspx"
    9.                 Else
    10.                     'already a user
    11.                     lblMessage.ForeColor = Color.Red
    12.                     lblMessage.Text = "This user already is registered in the IMS system!"
    13.                 End If

    Jon

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    7
    Thank you, your example helped out alot. Everything works well in IE6.0 but not Mozilla or nutscrape. I want to use the domvalidation controls, but am having a hard time uderstanding how to get them to work. Surely their must be an easy way to validate if a field in empty or an email is correct without going through all of this>

    Microsoft!

  6. #6
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by brutis2ka13
    Thank you, your example helped out alot. Everything works well in IE6.0 but not Mozilla or nutscrape. I want to use the domvalidation controls, but am having a hard time uderstanding how to get them to work. Surely their must be an easy way to validate if a field in empty or an email is correct without going through all of this>

    Microsoft!
    Validation controls work in IE 5+

    The next release of VS.net the patch will take care of other browsers such as mozillas / netscapes.

    You can validate anything, I dont know what you mean by "Going through all this"...

    Regular Expression Validatior is what you need for email.

    Try this:
    \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

    As your validation expression for an email.

    Jon

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    7
    Originally posted by jhermiz
    Validation controls work in IE 5+

    The next release of VS.net the patch will take care of other browsers such as mozillas / netscapes.

    You can validate anything, I dont know what you mean by "Going through all this"...

    Regular Expression Validatior is what you need for email.

    Try this:
    \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

    As your validation expression for an email.

    Jon
    Jon, I was reffering to getting the domvalidation controls to work. I really don't know if I should use the validation controls at all because I would like for validation to work with at least the top five browsers or so, not just IE. What I'm trying to do is simple enough, I think. I just want to make sure a user fills out a form with a few field like name and email that I want to make sure are filled in.

    Can this be done with if, then and display to a lable else?

  8. #8
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by brutis2ka13
    Jon, I was reffering to getting the domvalidation controls to work. I really don't know if I should use the validation controls at all because I would like for validation to work with at least the top five browsers or so, not just IE. What I'm trying to do is simple enough, I think. I just want to make sure a user fills out a form with a few field like name and email that I want to make sure are filled in.

    Can this be done with if, then and display to a lable else?
    Yes if you want you can avoid validation controls and you yourself can validate. However, this is a burden to you as a developer since it means you will have to right the code.

    If I were you I'd create a validator class, create an object of that class and call functions on what needs to be tested. In many types of tiered applications many developers include or write a dll / class that validates controls before pushing any data to the business tier (which then sends it to the db).

    You are using a tiered approach I assume? If so, it is very simple to create a class / dll and reference it in your code. Then before you call the business tier you validate the information. The validation is a simple get / set property which returns whether data exists. The nice thing about .net is the way that objects and the properties of those objects are created, you can also cast and wrap the objects into different types should you need various values.

    If you dont want to write a class simply create a function to validate a control on a page, if the condition is not true or the control doesnt meet your needs set IsValid to false. This way when you do a save to the database, do this before the save:

    If Page IsValid....then
    else...
    'dont save
    end if


    Jon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width