Input array is longer than the number of columns in this table.
  • When i run dis code it's show error

     string[] textData = System.IO.File.ReadAllLines(@"E:\DataReader\brethalizer5.txt");
            //string[] headers = textData[0].Split('\t');

            DataTable dt = new DataTable();
            //foreach (string header in headers)
            //    dt.Columns.Add(header, typeof(string), null);
            for (int i = 1; i < textData.Length; i++)
                dt.Rows.Add(textData[i].Split(','));

            GridView1.DataSource = dt;
                GridView1.DataBind();

    ERROR
    Input array is longer than the number of columns in this table.


    File Data is
    25 4 abc 2 2 25/5/2012 Fedex 2015 Abc  

                  

    Thank You
    Regards,
    M.Hussain
    Software Engineer
  • 1 Comment sorted by
  • 1) if you want to fill datatable from text file . you first add colums and rows into datatable .your mistake is this you are not define the colum name where data show without colum name where data show

    2) then in for each loop create rows for all text in file then read add it in dt like this ( This is vb.net code I hope you only want logic

    Dim dt As New DataTable()
    Dim col As New DataColumn("Name")
    Dim col2 As New DataColumn("abc")

    ' col.DataType = System.Type.GetType("System.String")
    dt.Columns.Add(col)

    Dim aa As String() = File.ReadAllLines("D:\Faraz Documents\ZRG faraz Projects\a.txt")
    For Each item As String In aa
    Dim dr As DataRow = dt.NewRow()
    dr(0) = item.ToString()
    dt.Rows.Add(dr)
    Next
    DataGridView1.DataSource = dt


    for further Details and Logic of this type of example visit www.codecorner.somee.com
    for further details and related topics http://www.codecorner.somee.com/

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

    Optimum Creative