user4611865
user4611865

Reputation:

Can't add new row to a datatable [c#]

I have a dataset, which is stored in XML file. When application starts and XML file doesn't exists yet, I want to add some rows to my dataset and save it in XML. I use following code to do it:

        partiesDb = new Parties();

        //...
        DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object
        for (int i = 0; i < size; i++)
        {
            DataRow row = partyDsTable.NewRow(); // HERE THE CODE STOPS
            row["sth"] = sth;
            row["sth"] = "0";
            row["id"] = "1";
            if (!partyDsTable.Rows.Contains(row))
            {
                partyDsTable.Rows.Add(row);
            }
        }
        System.Windows.Forms.MessageBox.Show(partyDsTable.Rows[0]["sth"].ToString());
        partiesDb.WriteXml(path);

and nothing happens. Even messagebox doesn't show. The function just stop when creating a new row, and I don't know why. Can you help me?

[EDIT]: This code works fine:

            partiesDb = new Parties();
            DataTable dsTable = partiesDb.Tables["party"];
            DataRow dsRow = dsTable.NewRow();
            dsRow["name"] = "";
            dsRow["id"] = "";
            dsRow["votes"] = "";
            dsTable.Rows.Add(dsRow);
            partiesDb.WriteXml(partiesDbPath);

I don't know why this works, but when I add loop it doesnt...

Upvotes: 0

Views: 2465

Answers (3)

Shannon Holsinger
Shannon Holsinger

Reputation: 2351

Also, if you create a class with fields that match the items in your database table, you can do all kinds of things with reflection to make usre your data is always good:

public static DataTable create_DataTable_From_Generic_Class(Type t)
    {
        DataTable d = new DataTable();
        FieldInfo[] fI = t.GetFields();
        for(int i = 0; i < fI.Length; i++)
        {
            DataColumn dC = new DataColumn(fI[i].Name, fI[i].FieldType);
            d.Columns.Add(dC);
        }
        return d;
    }
    public static object[] Create_Datatable_Row_From_Generic_Class(Type t, object instance,DataTable dt)
    {

        FieldInfo[] f = t.GetFields();
        object[] ret = new object[f.Length];
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            ret[i] = t.GetField(dt.Columns[i].ColumnName).GetValue(instance);

        }
        return ret;

    }
    public static List<string[]> getParams(Type type, bool convertToSQL)
    {


        List<string[]> ret = new List<string[]>();
        Dictionary<string, object> properties = new Dictionary<string, object>();
        foreach (FieldInfo prop in type.GetFields())
            properties.Add(prop.Name, prop.FieldType);
        foreach (string key in properties.Keys)
        {
            string[] r = { key, properties[key].ToString().Replace("System.", "") };
            ret.Add(r);
        }
        if (convertToSQL)
        {
            return convertFromNetToSQLDataTypes(ret);

        }
        else
        {
            return ret;
        }
    }
    private static List<string[]> convertFromNetToSQLDataTypes(List<string[]> list)
    {
        foreach (string[] data in list)
        {
            data[1] = data[1].Replace("Int16", "tinyint");
            data[1] = data[1].Replace("Int32", "int");
            data[1] = data[1].Replace("Int64", "bigint");
            data[1] = data[1].Replace("Double", "float");
            data[1] = data[1].Replace("Boolean", "tinyint");
            data[1] = data[1].Replace("Double", "float");
            data[1] = data[1].Replace("Long", "bigint");
            data[1] = data[1].Replace("String", "varchar(100)");
        }
        return list;
    }
    public static object[] sql_Reader_To_Type(Type t, SqlDataReader r)
    {
        List<object> ret = new List<object>();
        while (r.Read())
        {
            FieldInfo[] f = t.GetFields();
            object o = Activator.CreateInstance(t);
            for (int i = 0; i < f.Length; i++)
            {
                string thisType = f[i].FieldType.ToString();
                switch (thisType)
                {
                    case "System.String":

                        f[i].SetValue(o, Convert.ToString(r[f[i].Name]));
                        break;
                    case "System.Int16":
                        f[i].SetValue(o, Convert.ToInt16(r[f[i].Name]));
                        break;
                    case "System.Int32":
                        f[i].SetValue(o, Convert.ToInt32(r[f[i].Name]));
                        break;
                    case "System.Int64":
                        f[i].SetValue(o, Convert.ToInt64(r[f[i].Name]));
                        break;
                    case "System.Double":
                       // Console.WriteLine("converting " + f[i].Name + " to double");
                        double th;
                        if (r[f[i].Name] == null)
                        {
                            th = 0;
                        }
                        else
                        {
                            if (r[f[i].Name].GetType() == typeof(DBNull))
                            {
                                th = 0;
                            }
                            else
                            {
                                th = Convert.ToDouble(r[f[i].Name]);
                            }
                        }
                        try { f[i].SetValue(o, th); }
                        catch (Exception e1)
                        {
                            throw new Exception("can't convert " + f[i].Name + " to doube - value =" + th);
                        }
                        break;
                    case "System.Boolean":
                        f[i].SetValue(o, Convert.ToInt32(r[f[i].Name]) == 1 ? true : false);
                        break;
                    case "System.DateTime":
                        f[i].SetValue(o, Convert.ToDateTime(r[f[i].Name]));
                        break;
                    default:
                        throw new Exception("Missed data type in sql select in getClassMembers class line 73");

                }
            }
            ret.Add(o);

        }
        return ret.ToArray();


    }

And on and on. You can use reflection like this to create tables that match your class, create insert or procedure calls automatically, and never use an incorrect data type or get a field out of order again.

Upvotes: 0

Shannon Holsinger
Shannon Holsinger

Reputation: 2351

DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object
    for (int i = 0; i < size; i++)// "size" does not look like it's set to anything. If "size" is 0, then the loop will not loop. 
    {

            partyDsTable.Rows.Add(new object[] {sth,"0","1"});//you should probably make the columns into the correct type if you are using integers.

    }
    System.Windows.Forms.MessageBox.Show(partyDsTable.Rows[0]["sth"].ToString());
    partiesDb.WriteXml(path);

Upvotes: 0

user1666620
user1666620

Reputation: 4808

You can't add something to a null variable. So, you need to instantiate the partyDsTable if it is null.

DataTable partyDsTable = partiesDb.Tables["party"]; //partiesDb is DataSet object

if(partyDsTable == null) // instantiate it
    partyDsTable = new DataTable();

for (int i = 0; i < size; i++)
{
    ....

Upvotes: 1

Related Questions