Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Problem updating Access database from a datagrid


Message #1 by "Grahame Hambleton" <grahame.hambleton@s...> on Thu, 20 Sep 2001 09:21:49
Dear all, I tried to adapt one of the exercises (datagrid6.aspx) supplied 

with the quickstart tutorial with the asp.net beta2. I have translated it 

into c# and hooked it up to an Access table. I have put it an error 

handler (see below) which reports the following when I attempt to update 

the record



The error number is: 0 Message: Parameter @course has no default value. 

Native: -542379020 Source: Microsoft JET Database Engine SQL: 3748 

SQLstring: UPDATE students SET blob = @blob, course = @course, surname = 

@surname, yr = @yr where blob = @blob 



blob is the unique key field and is of type string.



offending function reproduced below. Database only has these four fields



Can anyone help? The original example (using the sql server works fine)

It's driving me nuts!



reg'ds



Grahame





 public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)

    {

        

        String updateCmd = "UPDATE students SET blob = @blob, course = 

@course, surname = @surname,  yr = @yr where blob = @blob";

        

       



        OleDbCommand myCommand = new OleDbCommand(updateCmd, myConnection);

        

        



   

    

       

		myCommand.Parameters.Add(new OleDbParameter("@blob", 

OleDbType.Char, 30));

       myCommand.Parameters.Add(new OleDbParameter("@course", 

OleDbType.Char, 20));

       myCommand.Parameters.Add(new OleDbParameter("@surname", 

OleDbType.Char, 50));

       myCommand.Parameters.Add(new OleDbParameter("@yr", OleDbType.Char, 

15));

        

       

        

        



        myCommand.Parameters["@blob"].Value = MyDataGrid.DataKeys[(int)

e.Item.ItemIndex];



        



        myCommand.Connection.Open();



        try

        {

            myCommand.ExecuteNonQuery();

            Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;

            MyDataGrid.EditItemIndex = -1;

        }

        catch (OleDbException exc)

        {

            if (exc.Errors.Count == 2627)

            

                Message.InnerHtml = "ERROR: A record already exists with 

the same primary key";

            else

            {

            for(int i=0; i< exc.Errors.Count; i++)

            	{

            	

            Response.Write("The error number is: " + i + " ");

            Response.Write("Message: " + exc.Errors[i].Message + "\n");

            Response.Write("Native: " + exc.Errors[i].NativeError.ToString

() + "\n");

            Response.Write("Source: " + exc.Errors[i].Source + "\n");

            Response.Write("SQL: " + exc.Errors[i].SQLState + "\n");

            Response.Write("SQLstring: " + updateCmd + "\n");

            	} //end for loop

            

                Message.InnerHtml = "ERROR: Could not update record, 

please ensure the fields are correctly filled out";

            Message.Style["color"] = "red";

             } // end of else


  Return to Index