0

My code works well when I'm adding less than 30 rows. But It can't handle more than that. How can I overcome this?

I get this error:Unspecified error when I'm trying to add less than 30, all the rows are added. and when I'm trying to add more than 30, it doesn't add nothing, and I get the error the number of the rows times.

Here is the code:

                    for (int i = 0; i < st1.Length; i++)
                {
                    UpdateDataBase(st1[i]);
                }

private void UpdateDataBase(char letter)
    {
        letter = char.ToUpper(letter);
        int serialPro = 0;
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                  "Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
        OleDbConnection connection = new OleDbConnection(connectionString);

        string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
        OleDbCommand command = new OleDbCommand(sql, connection);
        try
        {
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();
        //get the last!
        while (reader.Read())
            serialPro = reader.GetInt32(reader.Depth);


        sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
           + " values (?, ?)";
        OleDbCommand command2 = new OleDbCommand(sql, connection);

        command2.CommandType = CommandType.Text;
        command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
        command2.Parameters.AddWithValue("orderAACodon1", letter);
        command2.ExecuteNonQuery();
            }
        catch (Exception e)
        {
            MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
            this.Close();
        }
    }`

EDIT: private void UpdateDataBase(char letter) { letter = char.ToUpper(letter); int serialPro = 0; string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\Projects_2012\Project_Noam\Access\myProject.accdb";

        try
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();

                string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
                using (OleDbCommand command = new OleDbCommand(sql, connection))
                using (OleDbDataReader reader = command.ExecuteReader())
                {
                    //get the last!
                    while (reader.Read())
                        serialPro = reader.GetInt32(0);
                }

                sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
       + " values (?, ?)";
                using (OleDbCommand command2 = new OleDbCommand(sql, connection))
                {
                    command2.CommandType = CommandType.Text;
                    command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
                    command2.Parameters.AddWithValue("orderAACodon1", letter);
                    command2.ExecuteNonQuery();
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
        }
    }
enter code here
2
  • 1
    It doesn't add nothing? So what does it add? Commented May 21, 2012 at 8:52
  • nothing.but when the number of the rows is small, it adds all the rows. Commented May 21, 2012 at 9:00

3 Answers 3

1

I don't know for sure, but your OleDbConnection is never closed
Try the using statement which guarantees the closing and disposing of your connection

using(OleDbConnection connection = new OleDbConnection(connectionString))
{
    string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";         
    OleDbCommand command = new OleDbCommand(sql, connection);         
    try 
    {
     ......
    }
    catch(....)
}

also is not clear what your intent is in this line

serialPro = reader.GetInt32(reader.Depth); 

GetInt32 takes an int which is the ordinal number of the column required, while reader.Depth gets a value that indicates the depth of nesting for the current row.
I don't understand how the two values are related.

EDIT: In case you are looking for the max value of you serial number you could change the code in this way

string sql = "SELECT MAX(proInfoSerialNum) AS maxSN FROM tblProInfo ";                 
using (OleDbCommand command = new OleDbCommand(sql, connection))                 
{                     
    serialPro = (int)command.ExecuteScalar();                 
} 
Sign up to request clarification or add additional context in comments.

5 Comments

see what I wrote in @ThorstenDittmar answer
I fixed that error. SEE my EDIT How my code looks like now. I still get the error :Unspecified error
Could it be something with the tblOrderAA table. Do you have index or unique keys on your rows and trying to add duplicate records?
I don't think so.it is allowing to write the same number in orderAASerialPro
I don't see anything wrong. The only thing that could be changed is removing of the reader for the serial number if it is correct to assume that you are looking for the highest value in the table.
0

You might start by closing the database connections when you're done and you might get better results. Use the using statement to do so:

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    ...
}

The using statement makes sure that - for objects that implement the IDisposable interface - the Dispose method is called.

Also, the reader you're creating always stays open. So in the end, if rewritten, the method should look as follows:

private void UpdateDataBase(char letter)
{
    letter = char.ToUpper(letter);
    int serialPro = 0;
    string connectionString = "...";

    try
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();

            string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
            using (OleDbCommand command = new OleDbCommand(sql, connection))
            using (OleDbDataReader reader = command.ExecuteReader())
            {
                //get the last!
                while (reader.Read())
                    serialPro = reader.GetInt32(reader.Depth);
            }

            sql = "INSERT INTO ...";
            using (OleDbCommand command2 = new OleDbCommand(sql, connection))
            {
                command2.CommandType = CommandType.Text;
                command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
                command2.Parameters.AddWithValue("orderAACodon1", letter);
                command2.ExecuteNonQuery();
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
    }
}

To speed things up: For every call you're selecting all serial numbers and then iterate over the reader to get the latest. Is there an index you can use to put this work into the database? Like: Order by some date or select MAX(proInfoSerialNum)? Otherwise your software will slow down significantly when there are more serial numbers in the database.

10 Comments

now I get this error:Format of the initialization string does not conform to specification starting at index 0
Sounds like the connection string is malformed. Of course I've shortened the text constants a little. Please put some effort into trying to understand the changes instead of simply copying and pasting it.
I fixed that error. SEE my EDIT How my code looks like now. I still get the error :Unspecified error
Up to which line are you able to execute the code? Did you debug? Does the Inner Exception show anything more?
"to debug" is the process of finding bugs, not just pressing F5. Did you set a breakpoint and single-step through the method to see where the error occurs? Is there any more information about the error itself?
|
0
    void dataread()
    {
        query = "select * from nodes";
        cmd = new SqlCommand(query, con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {

            comboBox1.Items.Add(dr[0].ToString());
        }
        dr.Close();
        cmd.Dispose();

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.