Mike
Mike

Reputation: 2313

Error connecting to database ASP.NET C#

I keep getting this error: Format of the initialization string does not conform to specification starting at index 0.

This line of code:

using (OleDbConnection conn = new OleDbConnection("PayrollSystem_DBConnectionString"))

I think I need sql statements instead of Ole, I'm not sure.

Here is my form html code:

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

Here is my frmManageUsers code:

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;
        }
       // else

           // userName = (txtUserName.Text);


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

        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"); 

    }
    protected void drpdwnlstSecurityLevel_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Upvotes: 0

Views: 191

Answers (2)

Brian Deragon
Brian Deragon

Reputation: 2967

Also, if you're using a 64-bit system, you need to change the connection string to use the new provider, Microsoft.ACE.OLEDB.14.0

You can download it here:

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93464

OleDbConnection takes the actual connection string, not the NAME of the connection string. You have to get the connection string from the Configuration using ConfigurationManager.ConnectionStrings["PayrollSystem_DBConnectionString"].ConnectionString and pass that to OleDbConnection

Upvotes: 1

Related Questions