开发者

Cannot convert a row to a DataTable in C#

开发者 https://www.devze.com 2023-04-09 15:39 出处:网络
I\'m trying to convert a DataRow to a DataTable, but I\'m getting errors. I searched and tried all possible solutions, but none worked!

I'm trying to convert a DataRow to a DataTable, but I'm getting errors. I searched and tried all possible solutions, but none worked!

I have a method which accepts a DataTable as its parameter (this DataTable has one row exactly). That method will return some information.

At first, I tried to convert the DataRow to a DataTable using ImportRow(newtable.ImportRow(row)), but newtable is empty afterward. Then, I tried dt.clone(), but this fills the newtable with just everything, which is not what I was after! Actually the exact thing I was after.

private static void BatchFrontSidePrinting(Student St, frmBaseCard frm)
{
    DBINFOPACK UserInfo;
    DBCARDINFO CardInfo;
    DataTable newtable = new DataTable("newtable");

    foreach (DataRow row in dt.Rows)
    {
        try
        {

            // here, I'm trying to send one DataRow as a DataTable to the GetInfo() method,
            // and for the next iteratio , after getting the info I'm removing the row which was just added,
            // so that for the next iteration, newdatatable is empty.  All of the proceeding actions fail !:( 

            newtable.ImportRow(row); // doesn't work! 

            UserInfo = GetInfo(newtable);

            newtable.Rows.Remove(row); // doesn't work! 

            St = UserInfo.STUDENT;

            ((frmFrontSideCard)frm).Replica = UserInfo.CARDINFO.Replica;

            if (UserInfo.CARDINFO.Replica)
            {
 开发者_StackOverflow               Loger.Items.Add("Replication !");
            }

            // print
            ((frmFrontSideCard)frm).Print = St;

            // update
            CardInfo = UserInfo.CARDINFO;
            CardInfo.SID = UserInfo.STUDENT.ID;
            CardInfo.BIMAGE = UserInfo.BIMAGE;
            SetInfo(CardInfo);
        }
        catch (Exception exep)
        {
            Loger.Items.Add(String.Format("Inside [BatchFrontSidePrinting()] : Student {0} {1}:", St.ID, exep.Message));
        }
    }
}


foreach (DataRow row in dt.Rows)
{
    try
    {
        DataTable newtable = new DataTable();
        newtable = dt.Clone(); // Use Clone method to copy the table structure (Schema).
        newtable.ImportRow(row); // Use the ImportRow method to copy from dt table to its clone.
        UserInfo = GetInfo(newtable);

    catch (Exception exep)
    {
        //
    }
}


var someRow = newTable.NewRow();
someRow[0] = row[0]; // etc
newTable.Rows.Add(someRow);


It looks like you are using newtable as a temporary container to send each row in dt to the GetInfo method. If so, why not change the GetInfo method to take a DataRow rather than a DataTable that contains a single DataRow? Then you can get rid of newtable and not bother with creating and copying DataRows in the first place.

private static void BatchFrontSidePrinting(Student St, frmBaseCard frm) 
{ 
    DBINFOPACK UserInfo ; 
    DBCARDINFO CardInfo; 

    foreach (DataRow row in dt.Rows) 
    { 
        try 
        {       
            // just pass the row
            UserInfo = GetInfo(row); 

            // rest of the code as before
            St = UserInfo.STUDENT; 
            ((frmFrontSideCard)frm).Replica = UserInfo.CARDINFO.Replica; 
            if (UserInfo.CARDINFO.Replica) 
            { 
                Loger.Items.Add("Replication !"); 
            } 
            //print 
            ((frmFrontSideCard)frm).Print = St; 
            //update 
            CardInfo = UserInfo.CARDINFO; 
            CardInfo.SID = UserInfo.STUDENT.ID; 
            CardInfo.BIMAGE = UserInfo.BIMAGE; 
            SetInfo(CardInfo); 
        } 
        catch (Exception exep) 
        { 
            Loger.Items.Add(String.Format("Inside [BatchFrontSidePrinting()] : Student {0} {1}:", St.ID, exep.Message)); 
        } 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号