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
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Create Text File using C#
Pintoo Yadav
Sep 17
2015
Code
1.4
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
private
void
btnCBSUpload_Click(
object
sender, EventArgs e) {
string
M_FLNM, m_Str;
StreamWriter STRWRT;
// SqlDataReader drUpload;
if
(txtCBSAccNo.Text.Trim().Length == 0) {
MessageBox.Show(
"Please Enter CMS A/c No"
);
txtCBSAccNo.Focus();
return
;
}
if
(txtCBSupload.Text.Trim().Length == 0) {
MessageBox.Show(
"Please Enter File Description"
);
txtCBSupload.Focus();
return
;
}
//if (txtLOC_code.Text.Trim() == "")
//{
// MessageBox.Show("Please Enter Location");
// txtCBSupload.Focus();
// return;
//}
//if (txtDepoSiteSlip.Text.Trim() == "")
//{
// MessageBox.Show("Please Enter Deposite Slip no.");
// txtCBSupload.Focus();
// return;
//}
FLNM = @
""
+ lworkPath + DateTime.Today.ToString(
"ddMMyyyy"
);
if
(!Directory.Exists(FLNM)) {
Directory.CreateDirectory(FLNM);
}
else
{
Directory.CreateDirectory(FLNM);
}
// M_FLNM = @"C:\ADROIT\CTS\CBS_" + txtCBSupload.Text.Trim().ToUpper().Substring(0, Math.Min(4, txtCBSupload.Text.Length)) + "_" + DateTime.Today.ToString("ddMMyyyy") + ".txt";
//M_FLNM = lworkPath + DateTime.Today.ToString("ddMMyyyy") + "\\" + "CBS_" + txtCBSupload.Text.Trim().ToUpper().Substring(0, Math.Min(4, txtCBSupload.Text.Length)) + "_" + DateTime.Today.ToString("ddMMyyyy") + ".txt";
M_FLNM = lworkPath + DateTime.Today.ToString(
"ddMMyyyy"
) +
"\\" + "
CBS_
" + txtCBSupload.Text.Trim().ToUpper().Substring(0, Math.Min(4, txtCBSupload.Text.Length)) + "
_
" + DateTime.Today.ToString("
ddMMyyyy
") + "
.txt";
if
(!File.Exists(M_FLNM)) {
STRWRT =
new
StreamWriter(M_FLNM);
}
else
{
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(M_FLNM);
STRWRT =
new
StreamWriter(M_FLNM);
}
m_Str = (
" SELECT LEFT(ENDNO,5) AS MACHINEID,ENDNO AS SEQNO,CHEQUENUMBER AS CHQNO,BRANCHCODE AS SORTCODE,TRANSACTION_CODE AS TRCD, AMOUNT AS CHQAMT, "
+
" PRESENTATIONDATE AS BANKING_DATE FROM OUTWARDBATCHSCANDETAILS WHERE CONVERT(VARCHAR,PRESENTATIONDATE,103)='"
+ maskCleringDate.Text +
"' AND "
+
" (IsChequeStatus <> 'R' or IsChequeStatus is null) AND BATCHHID IN (SELECT DISTINCT BATCHID FROM OUTWARDBATCH WHERE CONVERT(VARCHAR,PRESENTATIONDATE,103)='"
+ maskCleringDate.Text +
"' AND "
+
" BATCHNO BETWEEN "
+ txtBatchFrm.Text +
" AND "
+ txtbatchToo.Text +
" AND BATCHSTATUSID=8) ORDER BY UPLOAD_ID "
);
string
maskCleringdt = Convert.ToDateTime(maskCleringDate.Text).ToString(
"yyyyMMdd"
);
//drUpload = pr_ExeDataReader(m_Str);
DataTable dt = clscom.GetTable4Command(m_Str);
if
(dt.Rows.Count == 0) {
MessageBox.Show(
"No Data Found.."
);
return
;
}
if
(dt !=
null
) {
//for (int i = 0; i < dt.Rows.Count; i++)
foreach
(DataRow item
in
dt.Rows) {
STRWRT.WriteLine(
string
.Format(
"{0,-16}{1,-3}{2,17}{3,-1}{4,-30}{5,16}{6,17}{7,-3}{8,-9}{9,-6}{10,-6}{11,-1}{12,-1}{13,-16}{14,-8}{15,-30}{16,-5}{17,-16}{18,-34}{19,-34}{20,-40}{21,-50}{22,-50}{23,-50}{24,-50}{25,-90}"
,
txtCBSAccNo.Text,
"INR"
, Convert.ToDecimal(item[
"CHQAMT"
]).ToString(
"0.00"
).Trim().PadLeft(17,
'0'
).Substring(0, 17),
""
, item[
"CHQNO"
], item[
"CHQNO"
], Convert.ToDecimal(item[
"CHQAMT"
]).ToString(
"0.00"
).Trim().PadLeft(17,
'0'
).Substring(0, 17), item[
"TRCD"
].ToString().PadRight(3,
' '
), item[
"SORTCODE"
],
" "
,
" "
,
"N"
,
"Y"
,
" "
, maskCleringdt,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
, txtCBSupload.Text,
" "
,
" "
,
" "
,
" "
));
}
}
else
{
MessageBox.Show(
"No Data Found.."
);
return
;
}
STRWRT.Close();
}
}
C#
Text Fil