none
Strong Typing Exception RRS feed

  • Question

  • Shouldn’t this work? According to the documentation it should.

           var query =
                from mp in abds.MemberPayments
                where mp.MemberNum == MemberNumSelected && mp.TransactionNum == transNo
                select new
                {
                   mpCheckNo = mp.CheckNumber??"",  // exception occurrs here.
                   mpFeeType = mp.FeeType??"",
                   mpAmount = mp.Amount,

                };
                
             foreach (var mpx in query)
             {
                CheckNoBox.Text = mpx.mpCheckNo??""; // with this comment out, it still fails.
                FeeTypeDropDownList.SelectedValue = Convert.ToString (mpx.mpFeeType);
                AmountBox.Text =  Convert.ToString (mpx.mpAmount);

             };

    I get The value for column 'CheckNumber' in table 'MemberPayments' is DBNull.

    StrongTypingException was unhandled by user code.

    But the ?? operator checks for null I thought.

    I have it set up as:

     

    AddressBookDataSet abds = new AddressBookDataSet ();

    AddressBookDataSetTableAdapters.

    MemberPaymentsTableAdapter abtaMemberPayments = new AddressBookDataSetTableAdapters.MemberPaymentsTableAdapter ();

    abtaMemberPayments.Fill (abds.MemberPayments);

    VS2008 .Net3.5

    Wednesday, April 28, 2010 10:10 PM

All replies

  • The strongly-typed DataSet predates nullable types.  You are seeing that the methods throw an exception instead of returning null in this case.

    Use the extension method, Field<typehere?>("ColumnName")

    You need to type the character "?" to mean nullable.  Replace "typehere" with a type such as int, decimal, etc.

    Once you have used the Field syntax, you will have a nullable type to which you can further apply ??.

    If you dislike that the use of "ColumnName" gets in the way of Intellisense, you can use yourDataSetInstance.TableName.ColumnNameColumn to get a DataColumn object to pass in its place.

    This is all rather awkward, but it is the way you have to do it.

     

    Thursday, April 29, 2010 2:01 AM
  • Hello,

     

    Welcome to ADO.NET DataSet forum!

     

    BinaryCoder is right.  The .Field<> extension method can help us solve the issue.  Additionally, if you are using typed dataset, we can set the NullValue property of the string typed column to (Empty) in the dataset designer, http://blogs.msdn.com/smartclientdata/archive/2005/08/13/NullValueForTypedDataSet.aspx.   

     

    For typed dataset, if the column’s AllowNull property is True, we get a generated method IsColumnNameNull() inside the typed table row class.    We can call this method to check if the value is DBNull instead of using ?? operator.  

     

    Hope these are helpful to you!

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 29, 2010 8:45 AM
    Moderator
  • I forgot to mention that CheckNumber is a string type and is already nullable. I didn't realise that I could set its nullvalue properties. So I set it to null and now CheckNumber??"" works. Knowing that the field was not set to a value by the end users is important.

    Next column in the table is smalldatetime. Setting its nullvalue to anything other than to throw an exception gives the error: The value entered is not valid for the current data type.

    I tried the technique, msprop:nullValue="_null" and then "_empty", shown in the link above for the database column [date] but neither of them work. It still threw an exception.

    So that leaves the .isColumnNameNull() technique, but I can not find where I would used that.

    The intellisense does not show this method for any of the variables in the select new block.

           var query =
                from mp in abds.MemberPayments
                where mp.MemberNum == MemberNumSelected && mp.TransactionNum == transNo
                select new
                {
                   mpCheckNo = mp.CheckNumber??"",  // exception occurrs here.
                   mpFeeType = mp.FeeType??"",
                   mpAmount = mp.Amount,

    mpTransDate = mp.Date,

                };
                

     

    Thursday, April 29, 2010 8:50 PM
  • Hi,

    So you mean the ?? operator works on string typed columns?   But the DateTime typed columns, neither our solutions works?  

    Could you please provide us with more detailed information of the typed dataset that you are using?   If it is convenient for you, would you mind sending me a demo project for further investigation?   My mail address is v-micsun@microsoft.com.   Thank you very much!

    Have a nice weekend!


    Best Regards,
    Lingzhi Sun
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com.  


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, May 1, 2010 12:14 PM
    Moderator
  • Hi,

    Could you please tell us how is the problem now? 

    If you need any further assistance, please feel free to let me know.

    Have a nice day!


    Best Regards,
    Lingzhi Sun
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com.  


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, May 12, 2010 1:06 AM
    Moderator