Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Sqlcommand accept null values

Tags:

c#

sqlcommand

I'm trying to get data in a gridview from a database to show up in text boxes upon clicking and it works fine for the rows with no null data, although since my int columns have some null values my GetInt32 methods keep returning "Data is Null. This method or property cannot be called on Null values."

Is there a simple way to fix or work around this? Do I replace GetInt32 with another method? I'd like for the data that is null to show up blank/empty in the text boxes if possible. Here's my code if you have any suggestions, thanks.

    public ArrayList GetAllPersonnel(int WorkerID) {
        using (var connection = new SqlConnection(connectionString)) {
            connection.Open();
            String query = "Select * FROM Personnel WHERE WorkerID = " + WorkerID;

            using (var command = new SqlCommand(query, connection)) {
                var reader = command.ExecuteReader();
                var list = new ArrayList();
                while (reader.Read()) {
                    String firstname = reader.GetString(1);
                    String lastname = reader.GetString(2);
                    String occupation = reader.GetString(3);
                    String deployment = reader.GetString(4);
                    int disasterid = reader.GetInt32(5);
                    String location = reader.GetString(6);
                    int deployedhours = reader.GetInt32(7);
                    int resthours = reader.GetInt32(8);

                    list.Add(firstname);
                    list.Add(lastname);
                    list.Add(occupation);
                    list.Add(deployment);
                    list.Add(disasterid);
                    list.Add(location);
                    list.Add(deployedhours);
                    list.Add(resthours);
                }
                connection.Close();
                reader.Close();
                return list;
            }
        }
    }
like image 791
Nick V Avatar asked Mar 25 '23 03:03

Nick V


2 Answers

You should use IsDBNull method of the SqlDataReader

int resthours = (!reader.IsDBNull(8) ? reader.GetInt32(8) : 0);

or, more directly

list.Add((!reader.IsDBNull(8) ? reader.GetInt32(8).ToString(): string.Empty));

Said that, I have noticed that you use a string concatenation to build the sql command text to retrieve records. Please do not do that. It is very dangerous and could lead to Sql Injection

String query = "Select * FROM Personnel WHERE WorkerID = @wkID";
using (var command = new SqlCommand(query, connection)) 
{
    command.Parameters.AddWithValue("@wkID", WorkerID);
    var reader = command.ExecuteReader();
    ....
like image 182
Steve Avatar answered Apr 02 '23 06:04

Steve


OK, so you're effectively saying that everything you display should be a string type, which is fine, I'm just making that point because you stated you want even integers to show up as an empty string. So how about this code?

String firstname = reader.GetString(1);
String lastname = reader.GetString(2);
String occupation = reader.GetString(3);
String deployment = reader.GetString(4);
String disasterid = reader.IsDBNull(5) ? string.Empty : reader.GetString(5);
String location = reader.GetString(6);
String deployedhours = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
String resthours = reader.IsDBNull(8) ? string.Empty : reader.GetString(8);

list.Add(firstname);
list.Add(lastname);
list.Add(occupation);
list.Add(deployment);
list.Add(disasterid);
list.Add(location);
list.Add(deployedhours);
list.Add(resthours);

Now, the reason I stated that you want to leverage everything as a string is because the default value for a int is 0 and that wouldn't meet the empty text box requirement.

like image 40
Mike Perrenoud Avatar answered Apr 02 '23 06:04

Mike Perrenoud