Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2004, 03:37 PM
Authorized User
 
Join Date: Apr 2004
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting Records Question?

Hello

Im trying to insert data into 5 fields in a database. The problem Im having is the first 3 fields keep getting null values in the database. In other words its inserting data into the last 2 fields but not into the first 3. Its just leaving them blank.

Can anyone help me with problem? Here is my code.

  Public area(3), phone(3), e1, e2 As String


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtMsg As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnChange As System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents btnRegister As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            messageForm = CType(Context.Handler, WebForm1)
            'Dim area(3), phone(3), e1, e2 As String
            Dim i As Integer
            Dim theLine As String
            Response.Buffer = True

            area(1) = Request.Form("txtArea1") : phone(1) = Request.Form("txtPhone1")
            area(2) = Request.Form("txtArea2") : phone(2) = Request.Form("txtPhone2")
            area(3) = Request.Form("txtArea3") : phone(3) = Request.Form("txtPhone3")
            e1 = Request.Form("txtEmail1")
            e2 = Request.Form("txtEmail2")

            'create session variables
            Session("a1") = area(1) : Session("p1") = phone(1)
            Session("a2") = area(2) : Session("p2") = phone(2)
            Session("a3") = area(3) : Session("p3") = phone(3)
            Session("e1") = e1

            'Data validation
            'Loop through area and phone array checking for invalid data
            txtMsg.Text = ""
            For i = 1 To 3
                If area(i) <> "" Or phone(i) <> "" Then
                    If Not IsNumeric(area(i)) Or Not IsNumeric(phone(i)) Then
                        txtMsg.Text = "Invalid Data." & vbCrLf & "Please go to previous page and re-enter."
                        Exit Sub
                    End If
                End If
            Next

            'Checking if either email text boxes are blank
            If (e1 = "") Or (e2 = "") Then
                txtMsg.Text = "Email addresses are not fully completed." & vbCrLf & "Please go to previous page and re-enter."
                Exit Sub
            End If

            'Reset counter variable to 0 and populate Msg text box if email addresses match
            i = 0
            If e1 = e2 Then
                For i = 1 To 3
                    If phone(i) <> "" Then
                        theLine &= " Area: " & area(i) & " Phone: " & phone(i).Insert(3, "-") & vbCrLf
                    End If
                Next
                theLine &= " Email: " & e1
                txtMsg.Text = theLine
            Else
                txtMsg.Text = "Email addresses are not the same." & vbCrLf & "Please go to previous page and re-enter."
            End If
        End If
    End Sub


    Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Dim myCNN As New ADODB.Connection
        Dim mySQL As String
        Dim counter As Integer
        Dim registerDate As DateTime = Today
        Dim expireDate As DateTime = Today.AddYears(5)

        myCNN.Open("DSN=MyDoNotCall")
        counter = 0
        For counter = 1 To 3
            mySQL = "Insert INTO Master(PatronEmail,AreaCode,PhoneNumber,Registrati onDate,ExpirationDate)"
            mySQL = mySQL & "Values('" & e1 & "','" & area(counter) & "','" & phone(counter) & "','" & registerDate & "','" & expireDate & "')"
            myCNN.Execute(mySQL)
        Next


        myCNN.Close()
        myCNN = Nothing
    End Sub

All the help will be greatly appreciated.
Eric

 
Old May 12th, 2004, 09:55 AM
Registered User
 
Join Date: May 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Eric,

That is hapenning because you are retreiving the user input from your form fields on PageLoad and storing them in Form level public variables, but since this is a web application even public variables will loose their value when the code execution comes to the button click event later on (when ever the user clicks the button). You can solve this by retreiving all the user input on button click event.

Anyway, why are you performing the validation on PageLoad?

Manish Jadhav







Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Large Number of Records mjackson11 Access 2 December 8th, 2004 04:50 PM
Inserting Returned Records hugh@kmcnetwork.com SQL Language 2 November 4th, 2003 01:05 PM
Inserting records into a databae aadz5 ASP.NET 1.0 and 1.1 Basics 1 October 19th, 2003 10:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.