c# - 连接字符串错误

标签 c# asp.net

我有一个表单,在用户输入用户名和密码并单击添加按钮后,它应该将其添加到我的 tblUserLogin 中。现在它有一个错误说:

Format of the initialization string does not conform to specification starting at index 0. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Line 40:            // userPassword = (txtPassword.Text);
Line 41: 
Line 42:         using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString"))
Line 43: 
Line 44:          {

这是我的 frmManageUsers 表单的 html 代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"  
        ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"  
        ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"  

   SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]"> 
</asp:SqlDataSource> 

</div> 
    <div align="center"> 
    <asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label> 
<p> 
    <asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label> 
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 
</p> 
<p> 
    <asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label> 
    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> 
</p> 
        <p> 
            <asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label> 
            <asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"  
                onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged"> 
                <asp:ListItem>A</asp:ListItem> 
                <asp:ListItem>U</asp:ListItem> 
            </asp:DropDownList> 
</p> 
        <p> 
            <asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click1"  
    Text="Add User" />  
  </p> 
        <p> 
            <asp:Label ID="lblError" runat="server"></asp:Label> 
</p> 

    </div> 
<p> 
    &nbsp;</p> 
            <div align="center"> 
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False"  
    DataSourceID="SqlDataSource1"> 
    <Columns> 
        <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"  
            SortExpression="UserID"></asp:BoundField> 
        <asp:BoundField DataField="UserName" HeaderText="UserName"  
            SortExpression="UserName"></asp:BoundField> 
        <asp:BoundField DataField="UserPassword" HeaderText="UserPassword"  
            SortExpression="UserPassword"></asp:BoundField> 
        <asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"  
            SortExpression="SecurityLevel"></asp:BoundField> 
        <asp:CommandField ShowEditButton="True"></asp:CommandField> 
        <asp:CommandField ShowDeleteButton="True"></asp:CommandField> 
    </Columns> 
</asp:GridView> 
</form> 
</body> 
</html>

这是我的代码:

public partial class frmManageUsers : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    protected void btnAddUser_Click1(object sender, EventArgs e) 
    { 
        //string userName, userPassword; 

        if (txtUserName.Text == "" || txtUserName.Text == null) 
        { 
            lblError.Text = ("User Name may not be empty"); 
            lblError.ForeColor = System.Drawing.Color.Red; 
            return; 
        } 

        if (txtPassword.Text == "" || txtPassword.Text == null) 
        { 
            lblError.Text = ("Password may not be empty"); 
            lblError.ForeColor = System.Drawing.Color.Red; 
            return; 
        } 

        using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString")) 
        { 
            string insert = "Insert INTO tblUserLogin (UserName, UserPassword, SecurityLevel) Values (@UserName, @UserPassword, @SecurityLevel)"; 
            OleDbCommand cmd = new OleDbCommand(insert, conn); 
            cmd.Parameters.Add("@UserName", txtUserName.Text); 
            cmd.Parameters.Add("@UserPassword", txtPassword.Text); 
            cmd.Parameters.Add("@SecurityLevel", drpdwnlstSecurityLevel.SelectedValue);   
            cmd.ExecuteNonQuery();       
        } 

        Session["UserName"] = txtUserName.Text; 
        Session["Password"] = txtPassword.Text; 
        Session["SecurityLevel"] = drpdwnlstSecurityLevel.SelectedValue; 
        Server.Transfer("frmManageUsers.aspx"); 

        //Server.Transfer("grdUserLogin");  
    } 
}

最佳答案

您实际上需要将有效的连接字符串传递给连接构造函数:

OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString")
// this is not an actual connection string

查看 Connection Strings协助制作有效的连接字符串。

虽然它确实看起来像是您正试图从 web.config 文件中提取连接字符串。这样的事情会起作用:

using System.Configuration;

// ....

OleDbConnection conn = 
    new OleDbConnection(ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString);

关于c# - 连接字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8330736/

相关文章:

c# - 如何根据 gridview 中的列值制作组(部分)?

c# - OnclientClick 和 OnClick 不能同时工作?

c# - 我如何在C#和文本编辑器中获得相同的Regex结果?

C#:使用列日期对 DATATABLE 进行排序

java - Java 中使用 CFB NoPadding 模式进行 C# AES 加密

c# - 有没有更好的方法在 C# 中自定义 SOAP header

c# - ftp 子文件夹下载递归功能不起作用

c# - 指定引用类型的默认值

c# - 另存为 8 位 PNG

asp.net - 加密 web.config 的自定义部分