Connecting Tech Pros Worldwide Forums | Help | Site Map

c# / DataSet: Setting DataRowView value to DBNull

Newbie
 
Join Date: Feb 2008
Posts: 4
#1: Feb 21 '08
Hello,

I searched for quite a while for an answer to this, but with no joy. All the answeres seem to be how to deal with getting a DBNull value out of a database, not to put it in . Perhaps I am not using the right search terms, but here we go.

Background:
I have a database with 2 tables with the following pertinent fields:
  • EMPLOYEE
    • EMPLOYEE_ID (pk) (string)
  • VEHICLE
    • VEHICLE_ID (pk) (int)
    • EMPLOYEE_ID (fk) (string)

These are loaded up in to a typed data set called vEHICLE_MILEAGEDataSet
Inside of the VEHICLE table, the foreign key, EMPLOYEE_ID is nullable.

My problem is this: I want to be able to un-assign an employee from a vehicle, and to do that, I would set EMPLOYEE_ID to DBNull in the VEHICLE table getting the currently selected vehicle.

Here is my code to do this (with the unimportant bits taken out):
Expand|Select|Wrap|Line Numbers
  1.  
  2. VEHICLE_MILEAGEDataSet.VEHICLERow vehRow = (employeeVehicleBindingSource.Current as DataRowView).Row as VEHICLE_MILEAGEDataSet.VEHICLERow;
  3.  
  4. if (vehRow != null)
  5. {
  6. //Place a Null in the EmployeeID column for the current vehicle
  7. vehRow.EMPLOYEE_ID = DBNull.Value;
  8. }
  9.  
  10. //commit the changes
  11. this.Validate(); 
  12. this.employeeVehicleBindingSource.EndEdit();
  13. this.vEHICLETableAdapter.Update(this.vEHICLE_MILEAGEDataSet.VEHICLE);
  14.  
  15. //refresh the data
  16. this.vEHICLETableAdapter.Fill(this.vEHICLE_MILEAGEDataSet.VEHICLE);
  17.  
So, obviously the major problem is the attempt to set a string equal to DBNull. I get the error "Cannot convert type 'System.DBNull' to 'string' "

I tried using DBNull.Value.ToString(); but I think that is sending an empty string, which wouldn't match any of the entries in the EMPLOYEE table, and give an constraints exception.

So how do I do this? It seems like it should be pretty simple, but it has me stumped.

Thanks,

-dan

Newbie
 
Join Date: Feb 2008
Posts: 4
#2: Feb 22 '08

re: c# / DataSet: Setting DataRowView value to DBNull


Ok, I am not finding much to do it the way I was originally intending, but perhaps I can do this another way.

Instead of worrying about putting a Null into the database at the DataSet level, perhaps I could modify the TableAdapter's Update to check for an empty string during the update and put the null there.

I know how to set up a Command to do this, but how would I connect this to the Table adapter? Where does this code need to be? Or do I need to do this in pure SQL?

Here is the proposed command:
Expand|Select|Wrap|Line Numbers
  1. public static int UpdateVehicle(int vehicleID, string employeeID)
  2.         {
  3.             string sql = @"
  4.                 UPDATE VEHICLE
  5.                 SET
  6.                     EMPLOYEE_ID = @employeeID
  7.                 WHERE
  8.                     VEHICLE_ID = @vehicleID
  9.             ";
  10.             SqlConnection conn = ConnectionManager.GetConnectionObject();
  11.             conn.Open();
  12.             using (SqlCommand command = new SqlCommand(sql, conn))
  13.             {
  14.                 command.CommandType = CommandType.Text;
  15.                 command.Parameters.Add("@vehicleID", SqlDbType.Int).Value = vehicleID;
  16.  
  17.                 //Check to see if the employeeID is empty, if it is, use a NULL instead
  18.                 if (employeeID == String.Empty)
  19.                 {
  20.                     command.Parameters.Add("@employeeID", SqlDbType.VarChar).Value = DBNull.Value;
  21.                 }
  22.                 else
  23.                 {
  24.                     command.Parameters.Add("@employeeID", SqlDbType.VarChar).Value = employeeID;
  25.  
  26.                 }
  27.                 return (command.ExecuteNonQuery());
  28.             }
  29.  
Thanks,

-dan
Newbie
 
Join Date: Sep 2009
Posts: 5
#3: Sep 9 '09

re: c# / DataSet: Setting DataRowView value to DBNull


Its working..
DBNull.Value did the trick for me..
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Sep 9 '09

re: c# / DataSet: Setting DataRowView value to DBNull


I think its because you are addressing the column in a "string" context, instead of an object context as it is normally done. I didn't recognize your dataAdapter style of code
Reply