C# 连接字符串错误

C# 连接字符串错误,c#,asp.net,C#,Asp.net,我有一个表单,在用户输入用户名和密码并单击“添加”按钮后,它应该将其添加到我的tblUserLogin。现在它有一个错误,上面写着: 初始化字符串的格式不符合规范 从索引0开始。描述:发生未处理的异常 在执行当前web请求期间。请检查 堆栈跟踪以获取有关错误及其位置的详细信息 源于代码 异常详细信息:System.ArgumentException:的格式 初始化字符串不符合从处开始的规范 索引0 以下是我的frmManageUsers表单的html代码: <asp:SqlDataSour

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

初始化字符串的格式不符合规范 从索引0开始。描述:发生未处理的异常 在执行当前web请求期间。请检查 堆栈跟踪以获取有关错误及其位置的详细信息 源于代码

异常详细信息:System.ArgumentException:的格式 初始化字符串不符合从处开始的规范 索引0

以下是我的
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>

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

OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString")
// this is not an actual connection string
签出以帮助创建有效的连接字符串

但它看起来确实像是您试图从web.config文件中提取连接字符串。类似这样的方法会奏效:

using System.Configuration;

// ....

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

我想您应该使用变量,而不是字符串文字。试一试

using (OleDbConnection conn = new OleDbConnection(PayrollSystem_DBConnectionString))

也就是说,如果
PayrollSystem\u DBConnectionString
是一个包含连接字符串值的字符串变量。

它表示当前上下文中不存在名称“ConfigurationManager”。您需要使用System.Configuration名称空间,以便能够使用
ConfigurationManager
类。这是可行的,但现在我有一个错误@cmd.ExecuteNonQuery();它说ExecuteOnQuery需要一个开放且可用的连接。连接的当前状态是关闭的。@Mike在调用
cmd.ExecuteNonQuery()
之前,您需要调用
conn.Open()
。现在它可以工作了,但如果我输入“A”作为用户名,“A”作为密码,然后单击“添加用户”,则不会发生任何事情。但是,如果我返回,现在键入“B”作为用户名,键入“B”作为密码,然后单击添加用户,则会将前面的条目“A”和“A”添加到表中。好像是落后一个?如果说得通的话:)
using System.Configuration;

// ....

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