简体   繁体   中英

How to test for DBNull for any data type?

I've been looking at the web for few days now and seems not find a path to my error. In my code there is a method that calls my Get_Data to perform a simple select and populate a wpf datagrid. Now the problem I'm facing is, when the method is executed it throws an error for DBNull. The table allow some columns to have nulls.

Table:

[WorkId]         INT           IDENTITY (100, 1) NOT NULL,
[fk_CustId]      INT           NULL,
[CustPartNum]    NVARCHAR (25) NULL,
[QtyPieces]      INT           NOT NULL,
[WoDateCreated]  DATETIME      DEFAULT (getdate()) NULL,
[WoDeliverDate]  DATETIME      NOT NULL,
[WoCompleted]    BIT           NULL,
[WoCompleteDate] DATETIME      NULL,
PRIMARY KEY CLUSTERED ([WorkId] ASC),
FOREIGN KEY ([fk_CustId]) REFERENCES [dbo].[Customers] ([CustId])

clsDB:

public static SqlConnection DB_Connect()
    {
        // Created connection to local DB
        string cn_String = Properties.Settings.Default.ShopSystemConnectionString;
        SqlConnection cn_Connect = new SqlConnection(cn_String);
        if (cn_Connect.State != ConnectionState.Open) cn_Connect.Open();

        return cn_Connect;
    }

public static DataTable Get_Data(string SQL_Text)
    {
        SqlConnection cn_Connect = DB_Connect();

        DataTable table = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(SQL_Text, cn_Connect);

        adapter.Fill(table);
        if (table.Rows.Count == 0)
        {
            MessageBox.Show("No record available for the selected line");
        }

        return table;
    }

The error:
在此处输入图片说明

Any help or advice is greatly appreciated

Edit 1

XAML code

<DataGrid x:Name="gridCustWO" Height="190" Canvas.Left="293" Width="734" FontSize="11" BorderThickness="1" Background="#FF725D55" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding WorkOrders}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding WorkId}" Width="Auto" Header="ID"/>
            <DataGridTextColumn Binding="{Binding CustPartNum}" Width="Auto" Header="Part Number"/>
            <DataGridTextColumn Binding="{Binding QtyPieces}" Width="Auto" Header="Qty Total"/>
            <DataGridTextColumn Binding="{Binding WoDateCreated}" Width="Auto" Header="Order Created"/>
            <DataGridTextColumn Binding="{Binding WoDeliverDate}" Width="Auto" Header="Shipped"/>
            <DataGridTextColumn Binding="{Binding WoCompleteDate}" Width="Auto" Header="WO Completed"/>
        </DataGrid.Columns>
    </DataGrid>

Not 100% sure that this will work but my assumption is that by using DataGridTextColumns and binding the data, it tries to convert them to strings, one way around that is to change the datagrid to:

<DataGrid x:Name="gridCustWO" Height="190" Canvas.Left="293" Width="734" FontSize="11" BorderThickness="1" Background="#FF725D55" IsReadOnly="True" AutoGenerateColumns="True" />

and programatically set the ItemsSource and hide any columns and change the headers, with something like:

gridCustWO.ItemsSource = myDataTable.DefaultView;
// if you want to hide a column
gridCustWO.Columns[0].Visibility = Visibility.Hidden;

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM