我正在尝试在页面加载时预先填充DropDownList。我在运行时遇到关键字DataSource不正确,并且突出显示的代码行是我声明变量con的位置 - 新的SqlConnection(CS)。在LoadList过程中。精确错误:System.Data.dll中出现“System.ArgumentException”类型的异常,但未在用户代码中处理 其他信息:不支持关键字:'datasource'。
我对此非常困惑,并且未能找到关于该问题的大量文档。这是我的web.config标记,后面是aspx.vb页面的代码。 任何见解将不胜感激。
谢谢!
Web配置:
<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
答案 0 :(得分:0)
我认为它必须是两个字,也是为了访问本地主机上的数据库它应该说'localhost'而不是'local':
<add name="GoForGold" connectionString="Data Source=localhost;Database=Friends;Integrated Security=SSPI"/>
答案 1 :(得分:0)
您的问题是您的连接适用于使用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