TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Yogesh Vedpathak
670
1.4k
197.7k
i want write same data into textfile
Jun 27 2018 12:56 AM
file reading data from textfile but when match userinput data to textfile data,process just write only this entry to file ,but not read all lines so same entry again come into textfile ,he is not write that entry into textfile
here is my code
protected void Button1_Click(object sender, EventArgs e)
{
try
{
while (true)
{
//ClientScript.RegisterStartupScript(GetType(), "Alert", "alert('Please wait file is generating');", true);
Dictionary<string, string> dictionary;
StreamReader file = null;
// StreamReader File = null;
file = db.ReadFile(out dictionary, TextBox1.Text, file);
if (file == null)
{
dvIsFileExist.Visible = true;
lblIsFileExist.Visible = true;
lblIsFileExist.Text = "Movement File not exist,Please check the file name";
break;
}
List<string> lines = new List<string>();
string line = null;
while ((line = file.ReadLine().ToString()) != null)
{
string[] LineDetails = line.Split('|');
if (LineDetails[1].Trim() == TextBox3.Text.ToString().Trim() && LineDetails[4] == TextBox2.Text.ToString().Trim() && LineDetails[15] == TextBox4.Text.ToString().Trim())
// if (LineDetails[1].Trim() == TextBox3.Text.ToString().Trim() && LineDetails[4] == TextBox2.Text.ToString().Trim() && LineDetails[15] == TextBox4.Text.ToString().Trim()) && (LineDetails[2].Trim() == TextBox3.Text.ToString().Trim() && LineDetails[4] == TextBox2.Text.ToString().Trim() && LineDetails[15] == TextBox4.Text.ToString().Trim())
{
lines.Add(line);
var PostingDate = lines[0].Split(new[] { '|' }, StringSplitOptions.None);
if (DateTime.Now.Month == Convert.ToInt32(PostingDate[2].Substring(4, 2))||DateTime.Now.Month == Convert.ToInt32(PostingDate[3].Substring(4, 2)))
{
dvIsFileExist.Visible = true;
lblIsCurrentMonth.Visible = true;
lblIsCurrentMonth.Text = "This is a current month,Please check";
break;
}
NewGeneratedFileName = TextBox1.Text.ToString().Substring(0, 8) + DateTime.Now.ToString("ddMMyyHHMMss") + "R";
string filename = dictionary["Archivepath"] + NewGeneratedFileName + ".old";
for (int i = 0; i < lines.Count; i++)
{
var x = lines[i].Split(new[] { '|' }, StringSplitOptions.None);
x[2] = DateTime.Now.ToString("yyyyMMdd");
x[3] = DateTime.Now.ToString("yyyyMMdd");
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Seek(0, SeekOrigin.End);
string temp = TextBox1.Text.ToString();
if (temp.Substring(0, 4) == "MV_F")
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(x[0] + dictionary["Separator"] + x[1] + dictionary["Separator"] + x[2] + dictionary["Separator"] + x[3] + dictionary["Separator"] + x[4] + dictionary["Separator"] + x[5] + dictionary["Separator"] + x[6] + dictionary["Separator"] + x[7] + dictionary["Separator"] + x[8] + dictionary["Separator"] + x[9] + dictionary["Separator"] + x[10] + dictionary["Separator"] + x[11] + dictionary["Separator"] + x[12] + dictionary["Separator"] + x[13] + dictionary["Separator"] + x[14] + dictionary["Separator"] + x[15] + dictionary["Separator"] + x[16] + dictionary["Separator"]);
}
}
else
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(x[0] + dictionary["Separator"] + x[1] + dictionary["Separator"] + x[2] + dictionary["Separator"] + x[3] + dictionary["Separator"] + x[4] + dictionary["Separator"] + x[5] + dictionary["Separator"] + x[6] + dictionary["Separator"] + x[7] + dictionary["Separator"] + x[8] + dictionary["Separator"] + x[9] + dictionary["Separator"] + x[10] + dictionary["Separator"] + x[11] + dictionary["Separator"] + x[12] + dictionary["Separator"] + x[13] + dictionary["Separator"] + x[14] + dictionary["Separator"] + x[15] + dictionary["Separator"]);
}
}
}
UserName = Session["UserName"].ToString();
FileName = TextBox1.Text.ToString();
Reason = DropDownList1.SelectedValue.ToString();
TransactionNumber = TextBox3.Text.ToString();
MaterialCode = TextBox2.Text.ToString();
LotNumber = TextBox4.Text.ToString();
cmd = new SqlCommand("insert into FileGenerator(FileName,TransactionNumber,MaterialCode,LotNumber,Reason,GeneratedMovementFileName,GeneratedBy,GeneratedDate) output INSERTED.MovementFileID Values('" + FileName + "','" + TransactionNumber + "','" + MaterialCode + "','" + LotNumber + "','" + Reason + "','" + NewGeneratedFileName + "','" + UserName + "',@GeneratedDate)", con);
cmd.Parameters.Add("@GeneratedDate", SqlDbType.DateTime).Value = DateTime.Now;
con.Open();
GRFileID = (int)cmd.ExecuteScalar();
if (GRFileID > 0)
{
IsLogged = db.LogFile(GRFileID, FileName, NewGeneratedFileName, UserName);
}
if (IsLogged)
{
IsSuccess = db.EmailSending(dictionary, NewGeneratedFileName, Reason);
}
if (IsSuccess != 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('File generated successfully');", true);
ClearControl();
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('There is some problem to generate Movement file');", true);
}
con.Close();
break;
}
//if (line.Contains(TextBox3.Text.ToString()))
//{
// if (line.Contains(TextBox2.Text.ToString()) && line.Contains(TextBox4.Text.ToString()))
// {
// lines.Add(line);
// break;
// }
// ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('Incorrect meterial code or Lot code');");
//}
}
break;
}
}
Reply
Answers (
2
)
How to Generate PDF corresponding Font Style
Learning New tech