My Table Adapter is returning null even after checking that the query is correct

江枫思渺然 提交于 2019-12-11 19:09:39

问题


I use a created query to return a row of values from a key, I have checked on the query executor but when running the program it is returning null. Please help me I'm starting to lose my mind.

I've tried debugging but the problem is coming from the fill data query and it is returning null.

        // Add ref. to the Dataset
        ViewMedia viewMediaSetInstance = new ViewMedia();
        // Create an empty Table
        ViewMedia.ViewMediaDataTable viewMediaTable = new ViewMedia.ViewMediaDataTable();
        // Create the Adapter we are going to use to populate the Table
        Model.ViewMediaTableAdapters.ViewMediaTableAdapter MediaTableAdapter = new Model.ViewMediaTableAdapters.ViewMediaTableAdapter();

        //Use query
        MediaTableAdapter.findByPublishYear(viewMediaTable, publishYear);

        //It doesn't find the row
        //I don't seem to find a solution even when the query returns the values in the executer.
        //viewMediaTable.Rows.Count == null so doesn't get inside
        if (viewMediaTable.Rows.Count > 0)
        {
            DataRow selectedUser = viewMediaTable.Rows[0];
            media = new Media(Int32.Parse(selectedUser["MediaID"].ToString()), selectedUser["Title"].ToString(), Int32.Parse(selectedUser["PublishYear"].ToString()));
            return media;
        }
        else
        {
            return null;
        }

I'm expecting to return the row of data to display in a book display.


回答1:


This is wonky; you've got strongly typed data rows with proper typed properties for everything, yet you're treating them as generic datarows with string column names, holding objects that you're tostring()ing and parsing. It was never intended to be used this way.

Your code should look more like this:

    ViewMedia viewMediaSetInstance = new ViewMedia();
    ViewMedia.ViewMediaDataTable viewMediaTable = new ViewMedia.ViewMediaDataTable();
    Model.ViewMediaTableAdapters.ViewMediaTableAdapter MediaTableAdapter = new Model.ViewMediaTableAdapters.ViewMediaTableAdapter();

    MediaTableAdapter.findByPublishYear(viewMediaTable, publishYear);

    //don't need to access the .Rows property to get the count
    if (viewMediaTable.Count > 0)
    {
        //don't need to access the .Rows property to get the data
        ViewMediaRow selectedUser = viewMediaTable[0];
        media = new Media(selectedUser.MediaID, selectedUser.Title, selectedUser.PublishYear);
        return media;
    }
    else
    {
        return null;
    }

I also disagree with your assertion in the code comments that datatable.Rows.Count is null. A datatable's .Count property is an integer regardless of whether a strongly or weakly typed datatable is in use; it can never be null

You don't need to create a dataset, if you enable creating a method that returns a datatable. Typically these methods are called GetDataByXXX and the fill methods are called FillByXXX (you've called your fill method findByPublishYear). It's configured on the following screen of the TA wizard:

image courtesy of nullskull.com

This can simplify your code to just the following (add a reference to Model.ViewMediaTableAdapters):

    var mDT = new ViewMediaTableAdapter().GetDataByPublishYear(publishYear);

    Media m = null;
    if (mDT.Count > 0)
        media = new Media(mDT[0].MediaID, mDT[0].Title, mDT[0].PublishYear);

    return m;

Most critically here, DON'T access the first row via the .Rows collection (viewMediaDataTable.Rows[0]) because that returns a base type DataRow. Access it directly via the default property indexer on the strongly typed datatable (viewMediaDataTable[0]) as this will return an object of type ViewMediaRow which in turn has nicely named, typed properties for all the columns in the datatable

viewMediaDataTable[0].PublishYear; //ViewMediaRow is a strongly typed class that has this property as an int
viewMediaDataTable.Rows[0].PublishYear; //"DataRow does not contain a definition for 'PublishYear'"

Now, if any of the data items in the row is null and the setting of the column upon null is "throw exception" you will see an exception of type StrongTypingException- it happens when you have some column that is of a type that cannot be null (such as an int) but it's null because the database query didn't return a value for that row. You were hitting this with your code because som int column (for example) was DBNull.Value and the only thing a datarow will do whn you try to access an int column tha tis DBNull, is throw an exception. By specifying SELECT * in the query, you caused the query to return a value for the column; by not selecting a column the datatable will always treat it as null. You can still also encounter a strongtypingexception if the value of the data in the database is also null. In these cases, use the IsXXXNull() methods to determine if the property is null before trying to access it (causing a strongtypingexception)




回答2:


Edit: Found the error, I was supposed to get all the columns from the query (using *) and then in the code take what I need otherwise null values get returned.



来源:https://stackoverflow.com/questions/57571231/my-table-adapter-is-returning-null-even-after-checking-that-the-query-is-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!