Unidata UniObjects for .NET - Write amendments back to unidata from modified table

777 Views Asked by At

I'm trying to write data back into a file on Unidata, after the contents have been adjusted in a datagridview.

I've tried various option based around the code below, but with no luck.

Within the foreach section I want to update my file. The file consists of 10 single value attributes.

I tried fl.write(),but get an error relating to writing to a null value...

     try
        {
            DataTable modifiedTable = m_DS.Tables[0].GetChanges(DataRowState.Modified);
            if (modifiedTable.Rows.Count > 0)
            {
                U2Connection con = GetConnection();
                Console.WriteLine("Connected.................");
                UniSession lUniSession = con.UniSession;
                UniFile fl = lUniSession.CreateUniFile("myTableName");
                UniDynArray udr3 = new UniDynArray(lUniSession);

                foreach (DataRow item in modifiedTable.Rows)
                {

                }
                con.Close();
             }
        }
2

There are 2 best solutions below

2
On BEST ANSWER

You will need to modify the UniDynArray (the record) for each row value in the table and then write the UniDynArray to the file and specific record id:

for (Int32 i=0; i < modifiedTable.Rows.Count; i++)
{
    DataRow item = modifiedTable.Rows[i];
    //Modify each attribute in the record from the rows in the table
    udr3.Replace(i+1, (String)item[0]);
}
//Write the modified record to the file
fl.Write("MyRecordId", udr3);

The reason you got the null reference exception is because you didn't assign a value to fl.RecordId or fl.Record before calling fl.Write(). As you can see above, I prefer to use the overload of the Write method that takes record id and record data as parameters instead of setting the properties on the instance of UniFile.

1
On

Thank you for using UniObjects’s API of U2 Toolkit for .NET (formerly known as standalone UO.NET).

Yesterday (June 10th, 2014) , we have Released U2 Toolkit for .NET v2.1.0. Main features of U2 Toolkit for .NET v2.1.0 is

  • Native Visual Studio Integration

For other features, see this link http://blog.rocketsoftware.com/2014/05/access-nosql-data-using-sql-syntax-u2-toolkit-net-v2-1-0-beta/

Can you please try the same code ( 10 single value attributes) using SELECT and UPDATE. For your information, SELECT and UPDATE behind the scene calls UniFile Read and Write. These Samples are part of the installation. Go to these directories.

C:\Program Files (x86)\Rocket Software\U2 Toolkit for .NET\U2 Database Provider\samples\C#\UniData\NativeAccess\Select_SQL_Syntax

C:\Program Files (x86)\Rocket Software\U2 Toolkit for .NET\U2 Database Provider\samples\C#\UniData\NativeAccess\Update_SQL_Syntax

SELECT

private static void Select()
    {
        try
        {
            Console.WriteLine(Environment.NewLine + "Start...");
            ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
            ConnectionStringSettings cs = settings["u2_connection"];
            U2Connection lConn = new U2Connection();
            lConn.ConnectionString = cs.ConnectionString;
            lConn.Open();
            Console.WriteLine("Connected...");
            U2Command cmd = lConn.CreateCommand();

            //ID,FNAME,LNAME : Single Value
            //SEMESTER: Multi Value
            //COURSE_NBR,COURSE_GRD: MS


            cmd.CommandText = string.Format("SELECT ID,FNAME,LNAME,SEMESTER,COURSE_NBR,COURSE_GRD FROM STUDENT WHERE ID > 0 ORDER BY ID");

            U2DataAdapter da = new U2DataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Console.WriteLine(Environment.NewLine);
            ds.WriteXml(Console.Out);
            lConn.Close();
            Console.WriteLine(Environment.NewLine + "End...");
            Console.WriteLine(SUCCESS_MSG);

        }
        catch (Exception e2)
        {
            string lErr = e2.Message;
            if (e2.InnerException != null)
            {
                lErr += lErr + e2.InnerException.Message;
            }
            Console.WriteLine(Environment.NewLine + lErr);
            Console.WriteLine(FAIL_MSG);
        }
    }

Update

private static void Update_Using_DataSet()
    {
        try
        {
            Console.WriteLine(Environment.NewLine + "Start...");
            ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
            ConnectionStringSettings cs = settings["u2_connection"];
            U2Connection lConn = new U2Connection();
            lConn.ConnectionString = cs.ConnectionString;
            lConn.Open();
            Console.WriteLine("Connected...");
            U2Command cmd = lConn.CreateCommand();

            //ID,FNAME,LNAME : Single Value
            //SEMESTER: Multi Value
            //COURSE_NBR,COURSE_GRD: MS


            cmd.CommandText = string.Format("SELECT ID,FNAME,LNAME,SEMESTER,COURSE_NBR,COURSE_GRD FROM STUDENT WHERE ID={0} ORDER BY ID",ID);

            U2DataAdapter da = new U2DataAdapter(cmd);
            U2CommandBuilder builder = new U2CommandBuilder(da);
            da.UpdateCommand = builder.GetUpdateCommand();

            DataSet ds = new DataSet();
            da.Fill(ds);

            DataTable dt = ds.Tables[0];
            DataRowCollection lDataRowCollection = dt.Rows;
            int i = 1;
            foreach (DataRow item in lDataRowCollection)
            {
                item["FNAME"] = item["FNAME"] + "3";// modify single value
                item["SEMESTER"] = item["SEMESTER"] + "$";//modify multi-value
                item["COURSE_GRD"] = item["COURSE_GRD"] + "$";

                i++;

            }
            da.Update(ds);//use DataAdapter's Update() API

            //print modified value
            cmd.CommandText = string.Format("SELECT ID,FNAME,LNAME,SEMESTER,COURSE_NBR,COURSE_GRD FROM STUDENT WHERE ID={0} ORDER BY ID", ID); ;

            //verify the change
            U2DataAdapter da2 = new U2DataAdapter(cmd);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            Console.WriteLine(Environment.NewLine);
            ds2.WriteXml(Console.Out);

            //close connection
            lConn.Close();
            Console.WriteLine(Environment.NewLine + "End...");

            Console.WriteLine(SUCCESS_MSG);
        }
        catch (Exception e2)
        {
            Console.WriteLine(FAIL_MSG);
            string lErr = e2.Message;
            if (e2.InnerException != null)
            {
                lErr += lErr + e2.InnerException.Message;
            }
            Console.WriteLine(Environment.NewLine + lErr);
        }
    }