2011-11-30 73 views
2

我有一個表格,用戶輸入用戶名和密碼後,點擊添加按鈕,它應該將其添加到我的tblUserLogin。現在它有一個錯誤,指出:ConnectionString錯誤

初始化字符串的格式不符合規範 開始在索引0說明:未處理的異常當前Web請求的執行過程中發生 。請查閱 堆棧跟蹤以獲取有關該錯誤的更多信息以及代碼中源代碼的起始位置 。

異常詳細信息:System.ArgumentException:在 初始化字符串的格式不符合規範開始 索引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"); 
    } 
} 

回答

2

實際上,你需要將有效的連接字符串傳遞給連接構造:

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); 
+0

它表示名稱'ConfigurationManager'在當前上下文中不存在。 – Mike

+0

你需要'使用System.Configuration'命名空間才能使用'ConfigurationManager'類。 – 2011-11-30 18:19:49

+0

@Mike我編輯反映。 – 2011-11-30 18:20:21

0

我想你應該使用變量不是字符串文字。嘗試

using (OleDbConnection conn = new OleDbConnection(PayrollSystem_DBConnectionString)) 

那就是如果PayrollSystem_DBConnectionString是一個包含連接字符串值的字符串變量。