I want write a text file event log. but when i want add second record(from datagridview) to text file, it will replace 1 record.

private void btnAddGoods_Click(object sender, EventArgs e)
        {
            SqlCommand cmdCheckID;
            SqlDataReader dtrCheckID;


            foreach (DataGridViewRow r in dataGridView1.SelectedRows)
            {


                string PONO = r.Cells[0].Value.ToString();
                string DeliveryDate = r.Cells[1].Value.ToString();
                string Customer = r.Cells[2].Value.ToString();
                string Name = r.Cells[3].Value.ToString();
                string Item = r.Cells[4].Value.ToString();
                string Model = r.Cells[5].Value.ToString();
                string FGrade = r.Cells[6].Value.ToString();
                string Thk = r.Cells[7].Value.ToString();
                string T_Unit = r.Cells[8].Value.ToString();
                string Width = r.Cells[9].Value.ToString();
                string W_Unit = r.Cells[10].Value.ToString();
                string Length = r.Cells[11].Value.ToString();
                string L_Unit = r.Cells[12].Value.ToString();
                string Qty = r.Cells[13].Value.ToString();
                string StockIn = r.Cells[14].Value.ToString();
                string Shortage = r.Cells[15].Value.ToString();
                string ItemPerPackage = r.Cells[16].Value.ToString();
                string Package = r.Cells[17].Value.ToString();
                string Remain = r.Cells[18].Value.ToString();
                bool InternalOrder = Convert.ToBoolean(r.Cells[19].Value.ToString());
                bool Loading = Convert.ToBoolean(r.Cells[20].Value.ToString());
                bool Finished = Convert.ToBoolean(r.Cells[21].Value.ToString());
                bool Urgent = Convert.ToBoolean(r.Cells[22].Value.ToString());
                string ReceivedDate = r.Cells[23].Value.ToString();
                string Remark = r.Cells[24].Value.ToString();

                

                //Connectt to Database
                ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;



                //Code Insert Both values into database table
                SqlConnection con = new SqlConnection(ConnectionString);


                con.Open();


                string strChechID = "select * from PDC_FG where PONO='" + PONO + "' and Customer='" + Customer + "' and Model='" + Model + "' and FGrade='" + FGrade + "'and Thk='" + Thk + "'and Width='" + Width + "' and Length ='" + Length + "' and Item='" + Item + "'";
                cmdCheckID = new SqlCommand(strChechID, con);
                dtrCheckID = cmdCheckID.ExecuteReader();



                SqlCommand comm = new SqlCommand("insert into PDC_FG values(@PONO,@DeliveryDate,@Customer,@Name,@Item,@Model,@FGrade,@Thk,@T_Unit,@Width,@W_Unit,@Length,@L_Unit,@Qty,@StockIn,@Shortage,@ItemPerPackage,@Package,@Remain,@InternalOrder,@Loading,@Finished,@Urgent,@ReceivedDate,@Remark)", con);
                SqlCommand comm1 = new SqlCommand("Delete From PDC where PONO=@PONO and Model=@Model and FGrade=@FGrade and Thk=@Thk and Width=@Width and Length=@Length and Item=@Item", con);

                try
                {

                    if (dtrCheckID.HasRows)
                    {
                        MessageBox.Show("Duplicate Data!!!!!!");

                        dtrCheckID.Close();

                    }

                    else
                    {
                        dtrCheckID.Close();

                        comm.Parameters.AddWithValue("@PONO", PONO);
                        comm.Parameters.AddWithValue("@DeliveryDate", DeliveryDate);
                        comm.Parameters.AddWithValue("@Customer", Customer);
                        comm.Parameters.AddWithValue("@Name", Name);
                        comm.Parameters.AddWithValue("@Item", Item);
                        comm.Parameters.AddWithValue("@Model", Model);
                        comm.Parameters.AddWithValue("@FGrade", FGrade);
                        comm.Parameters.AddWithValue("@Thk", Thk);
                        comm.Parameters.AddWithValue("@T_Unit", T_Unit);
                        comm.Parameters.AddWithValue("@Width", Width);
                        comm.Parameters.AddWithValue("@W_Unit", W_Unit);
                        comm.Parameters.AddWithValue("@Length", Length);
                        comm.Parameters.AddWithValue("@L_Unit", L_Unit);
                        comm.Parameters.AddWithValue("@Qty", Qty);
                        comm.Parameters.AddWithValue("@StockIn", StockIn);
                        comm.Parameters.AddWithValue("@Shortage", Shortage);
                        comm.Parameters.AddWithValue("@ItemPerPackage", ItemPerPackage);
                        comm.Parameters.AddWithValue("@Package", Package);
                        comm.Parameters.AddWithValue("@Remain", Remain);
                        comm.Parameters.AddWithValue("@InternalOrder", InternalOrder);
                        comm.Parameters.AddWithValue("@Loading", Loading);
                        comm.Parameters.AddWithValue("@Finished", Finished);
                        comm.Parameters.AddWithValue("@Urgent", Urgent);
                        comm.Parameters.AddWithValue("@ReceivedDate", ReceivedDate);
                        comm.Parameters.AddWithValue("@Remark", Remark);


                        comm1.Parameters.AddWithValue("@PONO", PONO);
                        comm1.Parameters.AddWithValue("@Model", Model);
                        comm1.Parameters.AddWithValue("@FGrade", FGrade);
                        comm1.Parameters.AddWithValue("@Thk", Thk);
                        comm1.Parameters.AddWithValue("@Width", Width);
                        comm1.Parameters.AddWithValue("@Length", Length);
                        comm1.Parameters.AddWithValue("@Item", Item);


                            System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\log1.txt");
                            
                            writer.Write(DateTime.Now.ToString());
                            writer.Write(PONO);
                            writer.WriteLine(Item);
                            writer.Write(writer.NewLine);
                            writer.Close();
                            writer.Dispose();

                        
                        

                        comm.ExecuteNonQuery();
                        comm1.ExecuteNonQuery();
                        
                        

                        con.Close();

                        timerLoad.Enabled = true;
                        lblMessage.Text = (PONO + "Successful Insert!!!");
                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }

Recommended Answers

All 3 Replies

You need to append to the log file.
Also I recommend that you put the writer outside the foreach loop to save opening and closing it each iteration (the same goes for the Connection).
Something like this.

private void button4_Click(object sender, EventArgs e)
{
	// get log file writer
	System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\log1.txt", true); //<<-- must append to log file

	// process data
	foreach (DataGridViewRow r in dataGridView1.SelectedRows)
	{
		// ... get data
				
		// write to log file
		writer.Write(DateTime.Now.ToString());
		writer.Write(PONO);
		writer.WriteLine(Item);
		writer.Write(writer.NewLine);

		// ... 
	}

	// close and dispose of log writer
	writer.Close();
	writer.Dispose();
}

Thanks. It work for me. but if I wan always add new record at the above of old record. How can I do??

When working with files you are working with data streams. It is not possible to push data forward in such streams; it is possible to move the file position to the start, but this just overwrites the previous data.

The only way to insert data at the beginning of a file is to write to a new file and then append the old file to this.

If the original is a small text file then you could read all of it in to an array or list of strings and insert new strings there, then write the array/list back to the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.