Hi, I have a data table having data from DB
I need to add one more column and data to that table from a .txt file.
when i ran my code that new colum not coming as a first colum and and a extra column without any data is added in to the data table.
Please anyone help
my .txt file is like
NAME
----
val1
val3
val5
i need to have NAME column as 0th colum of data table.below is my code
C#
if (tableName == "NewData")
{var pathC = @"D:\files\Data\names.txt";var fileContents = File.ReadAllLines(pathC);var splitFileContents = (from f in fileContents select f.Split(':')).ToArray();int maxLength = (from s in splitFileContents select s.Count()).Max();for (int i = 0; i < maxLength; i++)
{
table.Columns.Add();
}foreach (var line in splitFileContents)
{
DataRow row = table.NewRow();
row.ItemArray = (object[])line;
table.Rows.Add(row);
}
return table; //NAMES column will be the first column}
Nitin SontakkeRank #264 · 9.6K reputationPosted Dec 8, 2021, 11:07 PM EST
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Dec 8, 2021, 11:46 PM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Dec 8, 2021, 11:59 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Dec 9, 2021, 1:39 AM EST
I believe that the example you have shown is bit misleading.
Please try to give the data in the question closest to the reality.
For example, one would ask why are you spliting the line with : when there is no colon in the line.
While adding columns, you are not giving names.
First I thought that you wanted to add a column which is NOT available in the source. But I don't think that is the case.
Please provie maximum information, closest to the reality. If we make any assumptions, our answers will obviously be wrong.
You can even send a meeting request on nitin.n.sontakke at gmail dot com and we can discuss on Google Meet.
Sachin SinghRank #66 · 26.2K reputationPosted Dec 9, 2021, 3:56 AM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Dec 9, 2021, 4:05 AM EST
hari
shiju
sam
Sachin SinghRank #66 · 26.2K reputationPosted Dec 9, 2021, 5:22 AM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Dec 9, 2021, 5:43 AM EST
if am not using stream reader (its a console app attached to main project) how can i change
string[] strResult = File.ReadAllLines(@"C:\\file.txt"); this will work?
using (StreamReader sr = new StreamReader(@"D:\files\Data\names.txt"))
{
string strResult = sr.ReadToEnd();
string[] result = strResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Sachin SinghRank #66 · 26.2K reputationPosted Dec 9, 2021, 5:45 AM EST
Sujeet RamanRank #1,329 · 1.3K reputationPosted Dec 9, 2021, 7:09 AM EST
Hi but i am getting index out of array exception.How we will fix?
this is what i have tried
if (tableName == "TestData")
{
var pathC = @"H:\files\Data\names.txt";
string[] result = File.ReadAllLines(pathC);
// string[] result = strResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
DataColumn Col = table.Columns.Add("Name", typeof(String));
Col.SetOrdinal(0); // set column to first position
int i = 0;
foreach (DataRow row in table.Rows)
{
//need to set value to NewColumn column
row["Name"] = result[i]; // INDEX OUT OF EXCEPTION HERE
i++;
}
}
Sachin SinghRank #66 · 26.2K reputationPosted Dec 9, 2021, 11:04 AM EST
Sachin SinghRank #66 · 26.2K reputationPosted Dec 9, 2021, 1:49 AM EST