Click here to Skip to main content
15,898,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here my dilema, I'm unable to update my table to modify my ID numbers. I'm trying to do this because sometimes I have to modify the ID number to do a union all with another table so my ID's are a unique number.

This is what I have.

C#
try
           {
               OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strdb + "\\db1.mdb");
               thisConnection.Open();
               OleDbCommand thisCommand = thisConnection.CreateCommand();
               thisCommand.CommandText = "SELECT WPID from table1";
               OleDbDataReader thisReader = thisCommand.ExecuteReader();
               while (thisReader.Read())
               {
                   int StartLoop =50;
                   int StopLoop = 150;
                   int answer = 0;
                       //UPDATE ROUTINE
                   using (OleDbCommand thisUpdateCommand = thisConnection.CreateCommand())
                   {
                       for (int i = StartLoop; i < StopLoop; i++)
                       {
                           answer = answer + i;
                       }
                       thisUpdateCommand.CommandText = "update table1 set WPID = " + answer;
                       thisUpdateCommand.ExecuteNonQuery();
                   }
               }
               thisReader.Close();
               thisConnection.Close();
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }


I'm trying to do something like an autoincrement without using autoincrement built into Access. I want to control the numbers that go into it from start to finish hence why I'm trying to use a startloop and stoploop numbers.

Does anyone have an idea how I can do an update in a table using sequencial numbers?
Posted
Updated 6-Nov-11 9:08am
v2
Comments
Amir Mahfoozi 6-Nov-11 15:09pm    
Did you get any error by this way ?

1 solution

try,

I have hard coded the starting number as 160 and change accordingly and hope the code is self explanatory

private void button1_Click(object sender, EventArgs e)
       {
           try{

               using (OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DATABASE\\Database1.mdb"))
               {
                   thisConnection.Open();
                   OleDbCommand thisCommand = thisConnection.CreateCommand();

                   OleDbDataAdapter thisAdapter = new OleDbDataAdapter("select ID from WPID", thisConnection);
                   DataSet ods = new DataSet();
                   thisAdapter.Fill(ods);
                   OleDbCommandBuilder thisCommandBuilder = new OleDbCommandBuilder(thisAdapter);

                   for (int i=0; i < ods.Tables[0].Rows.Count;i++)
                   {
                      //160 is my start number and  change accordingly
                      ods.Tables[0].Rows[i]["ID"] = 160 + i;

                   }

                   thisAdapter.Update(ods.Tables[0]);
                   MessageBox.Show("OK");
               }

            }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
       }



UPDATE -1 Solution added later based on OP's comment and revises the above solution.

private void button3_Click(object sender, EventArgs e)
      {
          try
          {

              using (OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DATABASE\\Database1.mdb"))
              {
                  thisConnection.Open();
                  OleDbCommand thisCommand = thisConnection.CreateCommand();

                  OleDbDataAdapter thisAdapter = new OleDbDataAdapter("select ID,WPID from table1", thisConnection);
                  DataSet ods = new DataSet();
                  thisAdapter.Fill(ods);
                  OleDbCommandBuilder thisCommandBuilder = new OleDbCommandBuilder(thisAdapter);

                  int intStart = 3003;
                  int intEnd = 3010;

                  for (int i = 0; i < ods.Tables[0].Rows.Count; i++)
                  {

                      ods.Tables[0].Rows[i]["WPID"] = intStart;
                      intStart = intStart + 1;
                  }

                  while (intStart <= intEnd)
                  {
                      DataRow x = ods.Tables[0].NewRow();
                      x["WPID"] = intStart;
                      x["ID"] = "Blank" + intStart;
                      ods.Tables[0].Rows.Add(x);
                      intStart = intStart + 1;
                  }

                  thisAdapter.Update(ods.Tables[0]);
                  MessageBox.Show("OK");
              }

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
 
Share this answer
 
v3
Comments
mzrax 6-Nov-11 18:59pm    
Hello Bala

I've run your code and it seems to be almost working. I run the code and step into each loop and the number is changing but then it skips over the line that says thisAdapter.Update and gives me an error of

Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.

This error message is a new one for me.

What does this mean?
mzrax 6-Nov-11 22:12pm    
Bala

I figured something out. The column in the table has to be the primary key. Is there any way that I could do the same thing for a none primary key column? My primary key has to be on another column in the table.

My other question is right now the count only goes for the number of rows that are in the table. What would I have to do to have the count go larger than what's already in the table? For example if I have 60 rows of data in the table right now the count would go from 1 to 60 but my end result really has to be 1 to x, x being whatever number I choose. So there has to be an insert after the update.

I know it's complicated but I've got some really weird restrictions based on other legacy databases and tables that can't be updated or changed for a few years to come. I'm just trying to make this work.

First thing I have to ask, how to I make this work for a non primarykey column??

The second would be how do I make this work to insert more numbers than rows?

Thanks
Bala Selvanayagam 7-Nov-11 7:30am    
what is your table structure ?
mzrax 7-Nov-11 10:02am    
The simple part of the table is all Access based
Column 1 -->
Name: WPID
DataType: Number
Field Size: Integer

Column 2-->
Name: ID
DataType: Text
Field Size: 12
PrimaryKey

The rest of the columns don't matter but ID column is tied to many things while my WPID that I've created altering the table has to be int in order to query some legacy tables in legacy software that can't be changed because I don't have the code for the program. I can see the tables but not the program code.

Basically I want to be able to do what you've done with the code you wrote but do it for a none PrimaryKey column. I also want it to insert the numbers in new rows after it's done the update. What I was thinking was compare what's already in the table and do an update statement and then do an insert statement for the remaining number rows required with the numbers that I picked.

The WPID column is the one that I have to put the numbers in.

Does this help??
Bala Selvanayagam 7-Nov-11 15:12pm    
So you have a primary key column called "ID" (Text Data type) in addition to the WPID column which needs updating in sequncial order.

My question to you is, you are saying you may have to insert new rows at times and when you insert new rows what will be the value for the "ID" column ? i know the value for WPID is the next number in the sequential order but what is the value for the "ID" column ?


Should be able to give you a solution once i get the answer to this question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900