473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

c# / DataSet: Setting DataRowView value to DBNull

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
Feb 21 '08 #1
3 10069
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
Feb 22 '08 #2
Its working..
DBNull.Value did the trick for me..
Sep 9 '09 #3
Plater
7,872 Expert 4TB
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
Sep 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
8
by: drg | last post by:
I have a DataSet with a DateTime field. I am using an EventRow r, to update it with values from a TextBox in a DataGrid. Suppose a DataGrid in edit mode had a field with value: 7/7/2005. Then the...
11
by: scorpion53061 | last post by:
Well I had a way to write an array to an excel spreadsheet but on a huge time critical run it failed iwth the dreaded HRESULT: 0x800A03EC error. It worked fine when i sampled the data to go in but...
2
by: mPiccoli | last post by:
how I can a Guid field on Null setting (DATASET)
2
by: paolol | last post by:
Hi, I wont to get the current row values from a dataset, any one know how to reach those data ?? At the moment I use a work around, but it's not good .. foreach (DataRow row in...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
0
by: SAL | last post by:
I'm curious if anyone else is having this problem. If I let the Dataset designer generate update commands for me, I don't seem to be able to get them to work without an exception getting thrown,...
9
by: GotDotNet? | last post by:
I have a dataset and I have to loop through it and some of the values for an insertition into the db. Some of the fields are integers and booleans but contain a NULL in the field. how can I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.