我参与了一个VB.NET项目,我在如何连接表之间遇到了问题。
我有访问数据库[database1]
表:T1,RequestDetails
T1: U_ID Name Address Phone
RequestDetails: U_ID RqNo Requestport country_of_request RqMethod
在第一页上,用户应输入他的信息Name Address Phone
。单击buttom时,此数据将插入数据库并导航到第二页。
在第二页上,用户应根据U_ID
我有3个下拉列表:Requestport
,country_of_request
和RqMethod
Andd也在单击按钮时,它应该插入数据并进入下一步。
一切都好;我分别在每个页面上工作。现在,我想在U_ID
和T1
中的RequestDetails
之间建立连接,以便从第1页和第2页开始连接数据。
我不知道如何解释问题我希望每件事都清楚。
我的第1页代码:
我构建连接类来做连接人员
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
然后在按钮中:
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
这里没有问题。
在按钮的第二页:
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
问题在于:
如果我填写数据并单击下一步,则显示错误,因为U_Id
未填写且不应为空
这意味着它应该从第1页开始阅读u_id
...我该怎么做?
答案 0 :(得分:0)
这看起来像ASP项目中的VB.NET代码。如果是这种情况,我会要求您至少将其放在标签中,但您也可以使用POST将U_ID发送到第二页。
如果这是打开第二个窗口的纯VB.NET应用程序,您应该能够使第二个窗口成为父窗口的子窗口,创建一个名为U_ID的全局公共变量,并且能够调用parent.U_ID(父级应该是理想的是原始表格的名称。)我认为理想情况下你也可以在ASP中使用父调用,但我自己从未尝试过。
我本来想要一些澄清,但我现在似乎还不能这样做。如果您需要确认它实际上是哪一个,那么我可以根据需要编辑一些示例代码。
修改强>
这比HTTP Post方法简单得多。如需参考,请阅读The msdn article。
在主窗体中创建以下内容。
Public ReadOnly Property U_ID() As Integer
Get
Return ID
End Get
End Property
然后追加你的这个功能(这是我能想到的最简单的方法:
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
现在你有一个公共变量,你的第二页可以这样读:
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
Public U_ID as Integer = PreviousPage.U_ID
试试吧。您应该能够访问上一页的U_ID。