0

I have a simple query to update the users information however i get the error stating 'format of the initialization string does not conform to specification at index 33' and it seems to highlight this specific code Connection.Close(); however im not sure why, here is the complete code:

public void AddNewUser()
{
    string filePath;
    try
    {
        filePath = (Application.StartupPath + ("\\" + DBFile));
        connection = new System.Data.OleDb.OleDbConnection((ConnectionString + filePath));
        connection.Open();
        System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand();
        command.Connection = connection;
         // ---set the user's particulars in the table---
        string sql = ("UPDATE enroll SET SSN=\'"
                        + (txtSSN.Text + ("\', " + ("FirstName=\'"
                        + (txtFirstName.Text + ("\', " + ("LastName=\'"
                        + (txtLastName.Text + ("\' "
                        + (" WHERE ID=" + _UserID))))))))));
        command.CommandText = sql;
        command.ExecuteNonQuery();
        MessageBox.Show("Student added successfully!", "Registered");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Error");
    }
    finally
    {
       connection.Close();
    }
}

EDIT:

Here are the file paths:

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Users\\Zack\\My Documents\\Test\\Database.mdb";

const string DBFile = "C:\\Users\\Zack\\My Documents\\Test\\Database.mdb";
Neel Bhasin
  • 749
  • 1
  • 7
  • 22
Zack
  • 7
  • 10
  • 6
    You have a SQL injection vulnerability. – SLaks Jan 07 '14 at 03:35
  • 2
    Your connection string sytnax is incorrect. View:http://stackoverflow.com/questions/8243008/format-of-the-initialization-string-does-not-conform-to-specification-starting-a or post a sample of what `filePath` evaluates to. – Nico Jan 07 '14 at 03:37
  • @Nico I've edited the question and added the filepaths – Zack Jan 07 '14 at 03:45
  • Maybe you're just missing a closing quote in your connect string. – Gabe Jan 07 '14 at 03:47

2 Answers2

0

Your command text is wrong and you should use parametirized queries, here is correct version:

command.CommandText = "UPDATE enroll SET SSN= @ssn, FirstName = @fname, LastName = @lastName WHERE ID = @id";
command.Parameters.AddWithValue("@ssn", txtSSN.Text);
command.Parameters.AddWithValue("@fname", txtFirstName.Text);
command.Parameters.AddWithValue("@lastName", txtLastName.Text);
command.Parameters.AddWithValue("@id", _UserID);

And connection string:

string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Zack\My Documents\Test\Database.mdb'";
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

Zack,

There are quite a number of issues with this code. Primarly if you were to run this (as SLacks states) you are open to sql injection attacks. (Read up on it).

First off.. Your connection string (based on your code) when run will be.

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Users\\Zack\\My Documents\\Test\\Database.mdb\\C:\\Users\\Zack\\My Documents\\Test\\bin\Debug\\C:\\Users\\Zack\\My Documents\\Test\\Database.mdb

Well that is a guess. You should be using the following (note your path is hard coded).

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\"";

const string DBFile = "Database.mdb";
//...
var connection = new System.Data.OleDb.OleDbConnection(ConnectionString)

If you wanted to make your connection string dynamic to the path try this.

string conString = string.Format(ConnectionString, Path.Combine(Application.StartupPath, DBFile));
var connection = new System.Data.OleDb.OleDbConnection(conString);

This should set your connection string properly to you application startup. Now you may find it more useful to work of the executing assembly path as opposed to the application startup (your call).

Next your queries are a mess. I have cleaned it up to use parameterized queries instead with the resulting code somthing like. (note this has not been tested).

const string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\"";

const string DBFile = "Database.mdb";
public void AddNewUser()
{
    string conString = string.Format(ConnectionString, Path.Combine(Application.StartupPath, DBFile));
    using (var connection = new System.Data.OleDb.OleDbConnection(conString))
    {
        try
        {
            string sql = "UPDATE enroll SET SSN=@ssn, FirstName=@firstName, LastName=@lastName WHERE ID=@userID";
            System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(sql, connection);
            command.Parameters.AddWithValue("@ssn", txtSSN.Text);
            command.Parameters.AddWithValue("@firstName", txtFirstName.Text);
            command.Parameters.AddWithValue("@lastName", txtLastName.Text);
            command.Parameters.AddWithValue("@userID", _UserID);

            connection.Open();
            command.ExecuteNonQuery();
            MessageBox.Show("Student added successfully!", "Registered");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error");
        }
        finally
        {
            connection.Close();
        }
    }
}

EDIT:

I created a test lab for the code above and all ran correctly. Let me know if you have any questions.

Cheers.

Nico
  • 12,493
  • 5
  • 42
  • 62
  • Cheers for the help, i tried it but it highlights the 'connection.Close();' and says that NullReferenceException was unhandled? – Zack Jan 07 '14 at 04:08
  • @Zack are you confident that all your values are set? If you set your debugger on the very first line and check each variable. Also can you post the full error message including stacktrace? That will tell us exactly where the problem is. – Nico Jan 07 '14 at 04:10
  • I must be referencing my datasource wrong, because when i remove the data source it works fine 100% however as soon as i try and refernce my database to the connection string data source i get the same errors – Zack Jan 07 '14 at 04:26
  • @Zack try hard coding your database location such as `Path.Combine("C:\\Users\\Zack\\My Documents\\Test", DBFile)` as when you application startup path is ..\bin\Debug\ If this works then check that the file database.mdb is set to content and copy always. – Nico Jan 07 '14 at 04:27
  • Here's a link to a screenshot of the error message i am receiving http://imageshack.com/a/img577/7348/8s8q.jpg – Zack Jan 07 '14 at 04:43
  • @Zack the error is coming from your connection string. The connection string is definitly not valid. Try setting the full connection string to a static setting and work from there. – Nico Jan 07 '14 at 04:45