Asp.net将数据从访问数据显示到文本框 [英] Asp.net display data from access data to text box

查看:96
本文介绍了Asp.net将数据从访问数据显示到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..

i有问题从访问数据到文本框显示数据但不显示任何想想我有vs2013和下面的代码:

aspx :


 <%@       语言  =  vb    AutoEventWireup   =  false    CodeBehind   =  WebForm1.aspx.vb    继承  =  WebApplication14.WebForm1   %>  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< / head >
< body >
< 表格 id = form1 < span class =code-attribute>
runat = server >
< div >

< / div >
< asp:TextBox ID < span class =code-keyword> = TextBox1 runat = server > < / asp:TextBox >
< asp:TextBox ID = TextBox2 runat = 服务器 > < / asp:TextBox >
< asp:标签 ID = Label1 runat = 服务器 文本 = 标签 > < < span class =code-leadattribute> / asp:Label >
< asp:标签 ID = Label2 runat = server 文字 = 标签 > < / asp:标签 >
< asp:SqlDataSource ID = SqlDataSource1 runat = server ConnectionString = <% $ ConnectionStrings:Database1ConnectionString %> ProviderName = <% $ ConnectionStrings:Database1ConnectionString.ProviderName %> SelectCommand = SELECT * FROM [表1] > < / asp:SqlDataSource >
< / form >
< / body >
< / html >







vb.net:

  Imports  System.Data 
Imports System.Data.SqlClient
Public WebForm1
继承 System.Web.UI.Page

私有 Sub getData( ByVal user < span class =code-keyword> As 字符串
Dim dt 正如 DataTable()
Dim connection 作为 SqlConnection( Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Mohammed\Documents\Database1.mdb
connection.Open()
Dim sqlCmd 作为 SqlCommand( SELECT * from TABLE1 WHERE UserID = @username,connection)
Dim sqlDa 作为 SqlDataAdapter(sqlCmd)
sqlCmd.Param eters.AddWithValue( @ username,user)
sqlDa.Fill(dt)
如果 dt.Rows.Count> 0 然后
TextBox1.Text = dt.Rows( 0 )( ID)。ToString
TextBox2.Text = dt.Rows( 0 )( PhoneNumber)ToString
Label1.Text = dt.Rows( 0 )( ID)。ToString
Label2.Text = dt.Rows( 0 )( PhoneNumber)。ToString
结束 如果
connection.Close()
结束 Sub

受保护的 Sub Page_Load( ByVal sender As 对象 ByVal e As EventArgs)
如果 Page.IsPostBack 那么
getData( Me .User.Identity.Name)
结束 如果
结束 Sub
结束 班级





我是什么尝试过:



我更改数据库并更改代码但收到错误,我收到此代码但无法正常工作。



谢谢提前

解决方案

ConnectionS trings:Database1ConnectionString %> ProviderName = <%

ConnectionStrings:Database1ConnectionString.ProviderName %> < span class =code-attribute> SelectCommand = SELECT * FROM [Table1] > < / asp:SqlDataSource >
< / form >
< /身体 >
< / html >







vb.net:

  Imports  System.Data 
Imports System.Data.SqlClient
公共 WebForm1
继承 System.Web.UI.Page

私人 Sub getData( ByVal user As 字符串
Dim dt As < span class =code-keyword> New DataTable()
Dim connection As SqlConnection( Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\Users \Mohammed \Documents\Database1.mdb
connection.Open()
Dim sqlCmd 作为 SqlCommand( SELECT * from TABLE1 WHERE UserID = @username,connection)
Dim sqlDa As SqlDataAdapter(sqlCmd)
sqlCmd.Parameters.AddWithValue( @ username,user)
sqlDa.Fill(dt)
如果 dt.Rows.Count> 0 然后
TextBox1.Text = dt.Rows( 0 )( ID)。ToString
TextBox2.Text = dt.Rows( 0 )( PhoneNumber)ToString
Label1.Text = dt.Rows( 0 )( ID)。ToString
Label2.Text = dt.Rows( 0 )( PhoneNumber)。ToString
结束 如果
connection.Close()
结束 Sub

受保护的 Sub Page_Load( ByVal sender As 对象 ByVal e As EventArgs)
如果 Page.IsPostBack 那么
getData( Me .User.Identity.Name)
结束 如果
结束 Sub
结束 班级





我是什么尝试过:



我更改数据库并更改代码但收到错误,我收到此代码但无法正常工作。



谢谢提前


看起来你会得到K不支持eyword:'provider'。 因为应用程序使用SqlConnection类访问Microsoft Access数据库时出错。它应该使用OleDbConnection类。



这里代码应该如何。

 私有  Sub  getData( ByVal  user  As   String 

Dim dt As DataTable()
' Dim connection As New SqlConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C: \ Users \Mohammed \Documents\Database1.mdb)
Dim connection 正如 OleDbConnection( Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Mohammed\Documents\Database1.mdb
connection.Open ()
' Dim sqlCmd As New SqlCommand(SELECT * from TABLE1 WHERE UserID = @username,connection )
Dim sqlCmd 作为 OleDbCommand( SELECT * from TABLE1 WHERE UserID = @username,connection)
' Dim sqlDa As New SqlDataAdapter(sqlCmd)
Dim sqlDa 作为 OleDbDataAdapter(sqlCmd)
sqlCmd。 Parameters.AddWithValue( @ username,user)
sqlDa.Fill(dt)
如果 dt.Rows.Count> 0 然后
TextBox1.Text = dt.Rows( 0 )( ID)。ToString
TextBox2.Text = dt.Rows( 0 )( PhoneNumber)ToString
Label1.Text = dt.Rows( 0 )( ID)。ToString
Label2.Text = dt.Rows( 0 )( PhoneNumber)。ToString
结束 如果
connection.Close()
结束 Sub





那么.aspx上的SqlDataSource是多余的?


hello..
i have problem to display data from access data to text box but dont display any think i have vs2013 and the code below:
aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication14.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" ProviderName="<%$ ConnectionStrings:Database1ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource>
    </form>
</body>
</html>




vb.net:

Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
    Inherits System.Web.UI.Page

    Private Sub getData(ByVal user As String)
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mohammed\Documents\Database1.mdb")
        connection.Open()
        Dim sqlCmd As New SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
        Dim sqlDa As New SqlDataAdapter(sqlCmd)
        sqlCmd.Parameters.AddWithValue("@username", user)
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then
            TextBox1.Text = dt.Rows(0)("ID").ToString
            TextBox2.Text = dt.Rows(0)("PhoneNumber").ToString
            Label1.Text = dt.Rows(0)("ID").ToString
            Label2.Text = dt.Rows(0)("PhoneNumber").ToString
        End If
        connection.Close()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            getData(Me.User.Identity.Name)
        End If
    End Sub
End Class



What I have tried:

i change th database and change the code but get error and i get this code but not work .

Thanks Advance

解决方案

ConnectionStrings:Database1ConnectionString %>" ProviderName="<%


ConnectionStrings:Database1ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource> </form> </body> </html>




vb.net:

Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
    Inherits System.Web.UI.Page

    Private Sub getData(ByVal user As String)
        Dim dt As New DataTable()
        Dim connection As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mohammed\Documents\Database1.mdb")
        connection.Open()
        Dim sqlCmd As New SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
        Dim sqlDa As New SqlDataAdapter(sqlCmd)
        sqlCmd.Parameters.AddWithValue("@username", user)
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then
            TextBox1.Text = dt.Rows(0)("ID").ToString
            TextBox2.Text = dt.Rows(0)("PhoneNumber").ToString
            Label1.Text = dt.Rows(0)("ID").ToString
            Label2.Text = dt.Rows(0)("PhoneNumber").ToString
        End If
        connection.Close()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            getData(Me.User.Identity.Name)
        End If
    End Sub
End Class



What I have tried:

i change th database and change the code but get error and i get this code but not work .

Thanks Advance


Look like you will be getting " Keyword not supported: 'provider'. " error since the application is using SqlConnection class to access the Microsoft Access database. It should utilize the OleDbConnection class.

Here how the code should look like.

Private Sub getData(ByVal user As String)

    Dim dt As New DataTable()
    ' Dim connection As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mohammed\Documents\Database1.mdb")
    Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mohammed\Documents\Database1.mdb")
    connection.Open()
    ' Dim sqlCmd As New SqlCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
    Dim sqlCmd As New OleDbCommand("SELECT * from TABLE1 WHERE UserID = @username", connection)
    'Dim sqlDa As New SqlDataAdapter(sqlCmd)
    Dim sqlDa As New OleDbDataAdapter(sqlCmd)
    sqlCmd.Parameters.AddWithValue("@username", user)
    sqlDa.Fill(dt)
    If dt.Rows.Count > 0 Then
        TextBox1.Text = dt.Rows(0)("ID").ToString
        TextBox2.Text = dt.Rows(0)("PhoneNumber").ToString
        Label1.Text = dt.Rows(0)("ID").ToString
        Label2.Text = dt.Rows(0)("PhoneNumber").ToString
    End If
    connection.Close()
End Sub



By the way is the SqlDataSource on the .aspx redundant?


这篇关于Asp.net将数据从访问数据显示到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆