Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void AddValue(string strValue)
{
      //get the maximum id for Lists first
      int MaxID = DataOperations.ReturnMaxIDInATable("Lists", connString);
      int iSqlStatus = 0;
      string query = "INSERT INTO Lists(ID, ListName, ListValue) 
         VALUES(@MaxID, @ListName, @ListValue)";
      MaxID++;
      OleDbConnection dbConn = new OleDbConnection(connString);
      OleDbCommand dbComm = new OleDbCommand();
      dbComm.Parameters.Clear();
      try
      {
                dbComm.CommandText = query;
                dbComm.CommandType = CommandType.Text;
                OleDbParameter IDParam = new OleDbParameter();
                IDParam.ParameterName = "@MaxID";
                IDParam.OleDbType = OleDbType.BigInt;
                IDParam.Value = MaxID;
                dbComm.Parameters.Add(IDParam);
                dbComm.Parameters.AddWithValue("@ListName", ListName);
                dbComm.Parameters.AddWithValue("@ListValue", strValue);
                dbComm.Connection = dbConn;
                DataAccess.HandleConnection(dbConn);
                iSqlStatus = Convert.ToInt16(dbComm.ExecuteNonQuery());
                //Now check the status
                if (iSqlStatus != 0)
                {
                    //DO your failed messaging here
                    //return false;
                }
                else
                {
                    //Do your success work here
                    //dbComm.
                    //return true;
                }
      }
      catch (Exception ex)
      {
                MessageBox.Show(ex.Message, "Error inserting value in "
                                            + ListName + ","
                                            + strValue);
                //return false;
      }
      finally
      {
                DataAccess.HandleConnection(dbConn);
      }
}
Posted
Updated 3-May-11 3:43am
v2

 
Share this answer
 
Hi,
Instead of using "IDParam.ParameterName = "@MaxID";" can you try to pass the @MaxId as dbComm.Parameters.AddWithValue("@MaxId", MaxID);.
 
Share this answer
 
Comments
Regie23 16-May-11 9:07am    
Tried this already but it didn't work. Same error
Why don't you just do it like this:

C#
string query = string.Format("INSERT INTO Lists(ID, ListName, ListValue) 
         VALUES({0}, '{1}', '{2}')", MaxID, ListName, strValue);


Then you don't need the parameters...

EDIT ===============

Why was this 1-voted?
 
Share this answer
 
v3
Comments
Regie23 16-May-11 9:17am    
Wow this worked! Thanks a lot.. But its kind of weird why the parameter declaration don't work.

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