【问题标题】:Populating a dropdownlist from a database troubles从数据库问题中填充下拉列表
【发布时间】:2014-02-08 23:28:18
【问题描述】:

我正在尝试在页面加载时预填充 DropDownList。当我运行不支持关键字 DataSource 时出现错误,并且突出显示的代码行是我声明变量 con - As New SqlConnection(CS) 的位置。在 LoadList 过程中。确切错误 :System.Data.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理 附加信息:不支持关键字:“数据源”。

我对此感到非常困惑,并且无法找到有关该问题的大量文档。这是我的 web.config 标记,后面是 aspx.vb 页面的代码。 任何见解将不胜感激。

谢谢!

网页配置:

<connectionStrings>
<add name="GoForGold" connectionString="datasource=local;database=Friends;integrated     security=SSPI"/>   
</connectionStrings>

aspx.vb:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Control
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    DropDownList1.Items.Clear()
    DropDownList1.Items.Add("Please Select")
    DropDownList1.AppendDataBoundItems = True
    If Not Me.IsPostBack Then
        LoadList()
    End If
End Sub


Protected Sub LoadList()
    Dim CS As    String=ConfigurationManager.ConnectionStrings("GoForGold").ConnectionString                                 
    Dim Query As String = "SELECT * FROM Name"
    Dim con As New SqlConnection(CS)
    Dim cmd As New SqlCommand

    cmd.CommandType = CommandType.Text
    cmd.CommandText = Query
    cmd.Connection = con

    Try
        con.Open()
        DropDownList1.DataSource = cmd.ExecuteReader()
        DropDownList1.DataTextField = "First_Name"
        DropDownList1.DataValueField = "Friend_ID"
        DropDownList1.DataBind()
    Catch ex As Exception
        Throw ex
    Finally
        con.Close()
    End Try
End Sub

End Class 

【问题讨论】:

    标签: asp.net vb.net drop-down-menu


    【解决方案1】:

    我认为它需要是两个词,而且要访问本地主机上的数据库,它应该说“localhost”而不是“local”:

    <add name="GoForGold" connectionString="Data Source=localhost;Database=Friends;Integrated Security=SSPI"/>   
    

    【讨论】:

    • 刚试过,现在它说的是以下错误..(也许我需要命名方面的帮助)App_Web_4wo4ok12.dll 中发生了“System.Data.SqlClient.SqlException”类型的异常但未在用户代码中处理附加信息:在建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)
    • 等等,你应该也有'localhost'而不是'local'
    • 在这种情况下,您能否确保将我的答案标记为已接受,以便我获得积分?谢谢!
    【解决方案2】:

    您的问题是您的连接是针对 Oracle 的,它使用 Datasource

    Sql Server 连接字符串如下所示

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
    

    对于 Windows 身份验证使用如下:

     Server=myServerAddress;Database=myDataBase;Integrated Security=SSPI
    

    或者

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=Yes
    

    【讨论】:

    • 如果我使用 windows 身份验证,我可以排除用户 id 和密码,而只有服务器地址和数据库吗?另外,服务器地址是否只是我的 IP 地址?
    • 服务器是ip或者服务器名。使用 win auth,尽量不要设置 id/password。我想你不需要添加Trusted_Connection=Yes
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多