简体   繁体   中英

join between two tables from access database in two different page in vb.net

I worked on a VB.NET project and I have a problem in how to connect between tables.

I have access database [database1]

tables : T1 , RequestDetails

T1:               U_ID   Name   Address       Phone

RequestDetails:   U_ID   RqNo   Requestport   country_of_request    RqMethod

On first page, the user should enter his information Name Address Phone . When a buttom is clicked, this data is inserts into the database and navigates to the second page.

On the second page, the user should complete entering his data based on the U_ID

I have 3 dropdownlists: Requestport , country_of_request , and RqMethod

Andd also when a button is clicked, it should insert data and go next.

Everything's ok; I worked on each page in separate. Now I want to make connection between U_ID in T1 and RequestDetails to make data connected from page 1 and page 2.

I don't know how to explain problem I hope every thing was clear.

My code for page 1 :

I build connection class to do connection staff

Imports System.Data
Imports System.Data.OleDb
Imports System

Public Class connection

    Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hp\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\bin\Database1.accdb"
    Dim con As New OleDbConnection(str)
    Public Sub Insert(ByVal Name As String, ByVal Address As String, ByVal Phone As String)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim adp As New OleDbCommand("insert into T1 values(" & GetMaxID() & ",'" & Name & "','" & Address & "','" & Phone & "') ", con)

        adp.ExecuteNonQuery()
        con.Close()

    End Sub

    Public Function GetMaxID() As Integer

        Dim x As Integer = 1
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim adp As New OleDbCommand("select max(ID) from T1", con)
        Try
            x = adp.ExecuteScalar
            Return x + 1
        Catch ex As Exception
            Return x
        End Try
    End Function
End Class

Then in the button :

Public Class _Default
    Inherits System.Web.UI.Page

    Dim x As New connection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        x.Insert(TextBox1.Text, TextBox2.Text, TextBox3.Text)
        Response.Redirect("~/ReqDetails.aspx")
    End Sub
End Class

There is no problem here.

In the second page in the button:

Imports System.Data
Imports System.Data.OleDb
Imports System

Public Class shipmentDetails
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\hp\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\bin\Database1.accdb"
        Dim con As New OleDbConnection(str)

        con.Open()



        Dim Command As New OleDbCommand("INSERT INTO RequestDetails( Requestport," & "country_of_request," & "RqMethod,")VALUES(@Requestport,@country_of_request,@RqMethod)", con)"
        Command.Parameters.Add(New OleDbParameter("@Requestport", Requestport.SelectedItem.Text))
        Command.Parameters.Add(New OleDbParameter("@country_of_request", country_of_request.SelectedItem.Text))
        Command.Parameters.Add(New OleDbParameter("@RqMethod", RqMethod.SelectedItem.Text))
     )


        Command.ExecuteNonQuery()
        con.Close()    

        Label1.Text = "Thank You. Your transaction was successful."
        Label1.Visible = True 

    End Sub
End Class

Here is the problem:

If I fill the data and click next it shows me an error because U_Id not fill and it should not null

That means it should read u_id from the page 1...How can I do it?

This looks like VB.NET code within an ASP project. If that's the case, I'd ask you to at least put that in the tags, but you can also use POST to send the U_ID to page two.

If this is a pure VB.NET application opening a second window you should be able to make the second window a child of the parent, make a global public variable called U_ID and be able to call parent.U_ID (Parent should ideally be the name of your original form.). I think ideally you can use the parent call in ASP as well, but I've never tried it myself.

I would have actually asked for some clarification, but I can't seem to do that just yet. If you'd care to confirm which of the two it actually is then I could edit in a little sample code if you need.

EDIT:

Here is something considerably easier than the HTTP Post methodology. For reference, read The msdn article .

Create the following in the main form.

Public ReadOnly Property U_ID() As Integer
    Get
        Return ID
    End Get
End Property

Then append your one function like this (It's about the easiest way I can figure this to work:

Public ID as Integer

Public Function GetMaxID() As Integer

        Dim x As Integer = 1
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        Dim adp As New OleDbCommand("select max(ID) from T1", con)
        Try
            x = adp.ExecuteScalar
            ID=x+1
            Return x + 1
        Catch ex As Exception
            Return x
        End Try
    End Function

Now you have a public variable your second page can read like this:

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> 

Public U_ID as Integer = PreviousPage.U_ID

Try that. You should be able to access the previous page's U_ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM