tags:

views:

324

answers:

1

Here's the code :

try
        {
            string strReadDataLine;
            strReadDataLine = sr.ReadLine();

            while (strReadDataLine != null)
            {
                string[] strReadDataLineSplited = strReadDataLine.Split(';');

                DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow();
                DataTable item = thisDataSet.Tables["Repartition"];
                for (int i = 0; i < strDataLines.Length; i++)
                {
                    DataColumn thisColomn = 
                              thisDataSet.Tables["Repartition"].Columns[i];
                    // Here i need to know if the colomn is a string
                    if (thisColomn.DataType.ToString() == "System.String") 
                    {
                        thisRow[strDataLines[i]] = strReadDataLineSplited[i];
                    }
                }
                thisRow["ID_USAGER"] = 1;

                thisDataSet.Tables["Repartition"].Rows.Add(thisRow);

                strReadDataLine = sr.ReadLine();
            }
            //thisDataAdapter.Update(thisDataSet, "Repartition");

        }

What I need is to know if a column is a string to assign a data as string to the column. What I get is a argumentException saying "input string was not in correct format. couldn't store <2.111> in MY_FLOAT colomn. Expect type is double."

What I really need is to compare the column type to something to get the type then assign the column to the correct type.

I hope this is clear as my English is not so good.

A: 

If I understand correctly I built a functional copy of the code-fragment and fixed it to correctly handle the type conversion. You really only missed two things:

. #1 - You were indexing into the columns by ordinal and using that information to obtain the column type. Then setting the information you indexed the column my name. I corrected this with the introduction of the 'columnName' variable below.

. #2 - To properly convert the string input to the desired column type you only need to use the System.Convert.ChangeType(object, Type) method as shown below.

    static void Main()
    {
        DataSet ds = new DataSet();
        DataTable dt = ds.Tables.Add("Repartition");
        DataColumn col;

        col = dt.Columns.Add("ID_USAGER", typeof(int));
        col = dt.Columns.Add("TestString", typeof(string));
        col = dt.Columns.Add("TestInt", typeof(int));
        col = dt.Columns.Add("TestDouble", typeof(double));

        string testData = "TestString;TestInt;TestDouble";
        testData += Environment.NewLine + "Test1;1;1.1";
        testData += Environment.NewLine + "Test2;2;2.2";

        Test(ds, new StringReader(testData));
    }

    public static void Test(DataSet thisDataSet, StringReader sr)
    {
        string[] strDataLines = sr.ReadLine().Split(';');
        string strReadDataLine;
        strReadDataLine = sr.ReadLine();

        while (strReadDataLine != null)
        {
            string[] strReadDataLineSplited = strReadDataLine.Split(';');

            DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow();
            DataTable item = thisDataSet.Tables["Repartition"];
            for (int i = 0; i < strDataLines.Length; i++)
            {
                string columnName = strDataLines[i];
                //#1 Don't use this as Columns[i] may not be Columns[columnName]
                //DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[i];
                DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[columnName];
                //#2 Assing to the results of the string converted to the correct type:
                thisRow[strDataLines[i]] = System.Convert.ChangeType(strReadDataLineSplited[i], thisColomn.DataType);
            }
            thisRow["ID_USAGER"] = 1;

            thisDataSet.Tables["Repartition"].Rows.Add(thisRow);

            strReadDataLine = sr.ReadLine();
        }
    }
csharptest.net