Hello friend and expert ,
I have text file like this 48200120420110830I around 5000 record . The text file from bar code swipe machine.
The 48200 stand for employee code , 12042011 stand for 12 April 2011, 0830 stand for time 08:30 AM and I stand for In/Out ,I=In
I coding to get this text file by stream reader as code below
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Text Files(*.txt)|*.txt";
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
label1.Text = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.FileName);
After read the text file I would like to write the text file to Datagridview before write to Database.
I design table in database like this.
Emp_code nvarchar(2),
Morning_IN datetime,
Morning_Out datetime,
Afternoon_In datetime,
Afternoon_Out datetime,
Evening_IN datetime,
Evening_Out datetime
But I so confusing and I have question to asking for help as below.
1) How can I I write the text file (5000 record ) to Datagridview as my database table design ? Because the text file format
have a lot of duplicate employee code ? ( please forgive me if I not a good explain )
Emp_code Morning_IN Morning_Out Afternoon_In Afternoon_Out Evening_In Evening_Out
2) I intend to use the Linq to SQL classes . Please teach me how to write the text file to Datagridview and then can allow to edit data at Datagridview. After that click at the button to save to database .
Anyone help me please and this will me the most appreciated.
Pang Foon (Thailand )
Loading
VulpesPosted Jun 27, 2011, 1:47 PM
Sam HobbsPosted Jun 27, 2011, 1:21 PM
There are over 400 articles there, many of which will help you.
Morcos AdelPosted Jun 27, 2011, 4:57 AM
natchaya khamkunaPosted Jun 27, 2011, 4:44 AM
database easy create like this: id(bigint) auto increment Emp_code(varchar5) clock(datetime) IO(varchar 2)
my code as below :
private void btnOpenRead_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Text Files(*.txt)|*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.FileName);
ClockRecord = sr.ReadLine();
while (ClockRecord != "")
{
day = ConvertNumber(ClockRecord.Substring(5, 2));
month = ConvertNumber(ClockRecord.Substring(7, 2));
year = ConvertNumber(ClockRecord.Substring(9, 4));
hour = ConvertNumber(ClockRecord.Substring(13, 2));
minute = ConvertNumber(ClockRecord.Substring(15, 2));
dt = new DateTime(year, month, day, hour, minute, 0);
ClockRecord = sr.ReadLine();
}
}
}catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
static int ConvertNumber(string text)
{
int i;
if(Int32.TryParse(text,out i))
return i;
MessageBox.Show("Invalid data: ", text);
return 0;
}
______________________
I now can separated the string But I don't know how to write to database .
for example I try : (Linq)
Times t = new Times();
t.Emp_Code = ClockRecord.Readline.Substring(5,2);
This will write to database only one record. I don't know how to make a loop . Since do not have line to count .
Expert help me please ..
Pang (Thailand)
Sam HobbsPosted Jun 26, 2011, 3:49 PM
I suggest reading the file in and writing it to a table in the database. It will be easier to manage that way. You can use something like in my Database Table Update in a DataGridView without Writing Code article to edit the data and write it back.
Morcos AdelPosted Jun 26, 2011, 7:24 AM
i think that i can help you till the part of the Update :) coz i am not much an expert in the linq to sql classes....
i think as far as i understood you already read the text file if the text file is devided line by line or is separated by commas you can read it line by line or till the next comma and Save the read value in a string or something and then cut it out by using .substring(start_index,Length) that is if the numbers count you provided is constant for all employees.
after deviding into strings you can add the data you have into a data table like the following:
String x="here I am";
DataTable Data=new DataTable(); // Create Data Table
DataColumn Column=new DataColumn("column name"); //Creating Columns
Data.Columns.Add(Column); //Adding Columns To Table
DataRow Row=new DataRow(); //Creating Row
Row.beginedit(); //Begin Row Editing
Row["column name"]=x; //Add The Value to The Row of ["column name"]
Row.EndEdit(); //End Edit
Data.Rows.Add(Row); // Adding the Rows Created to the DataTable
datagridview.datasource=Data; // Bind the Data Grid
and you can select the type of the column when creating it :)
Soo i hope that this can solve your first problem of displaying the Text in the Grid