I need help here.I don't know how to tell to labels from different stringBuilder to go on different .doc files.In my if() statements in need to tell to write labels from first stringBuilder and in another if() statement should write labels from another stingBuilder in another .doc file. Below is my code:
StringBuilder strBody = new StringBuilder();
StringBuilder strBody1 = new StringBuilder();
strBody.Append(#"<html " +
"xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><title>Time</title>");
strBody1.Append(#"<html " +
"xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><title>Time</title>");
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" +
"<p style='color:red; font-size:13px'>" +
Label47.Text +"<br/>" +
Label45.Text +"X "+
Label48.Text +" " +
Label54.Text +"</p>" +
"</div></body></html>").Append(strBody1.ToString());
strBody1.Append("<body lang=EN-US style='tab-interval:.5in'>" +
"<p style='color:red; font-size:13px'>" +
Label12.Text +"<br/>" +
Label11.Text +"X "+
Label13.Text +" " +
Label17.Text +"</p>" +
"</div></body></html>");
if (Session["one"] != null && Session["two"] != null && Session["three"] != null)
{
{
string path = #"c:\Backup\kitchen.doc";
string path2 = #"c:\Backup\bar.doc";
if (!File.Exists(path) && !File.Exists(path2))
{
using (StreamWriter sw = File.CreateText(path))
{
using (StreamWriter sw2 = File.CreateText(path2))
{
if (Label53.Text == "4")
{
sw.WriteLine(strBody);
}
else if (Label53.Text == "1")
{
sw2.WriteLine(strBody);
if (Label44.Text == "4")
{
sw.WriteLine(strBody1);
}
else if (Label44.Text == "1")
{
sw2.WriteLine(strBody1);
}
}
I think there is a missing closing } in your if conditions. The
else if (Label53.Text == "1")
did not have a closing }
Updated if conditions
if (Label53.Text == "4")
{
sw.WriteLine(strBody);
}
else if (Label53.Text == "1")
{
sw2.WriteLine(strBody);
}
if (Label44.Text == "4")
{
sw.WriteLine(strBody1);
}
else if (Label44.Text == "1")
{
sw2.WriteLine(strBody1);
}
Related
Example .txt file output will be:
lastname|firstname|tintin|password|Manager
lastname|firstname|carley|password|Manager
If I try to register username 'tintin' and other data, it debugs it and says that the username already exists.
If I try to register username 'carley' and other data, the data is recorded in the .txt file even though there is already an existent carley username in the file. Please help.
private void btnregister2_Click(object sender, EventArgs e)
{
FileStream fStream = new FileStream("UserAccount.txt", FileMode.Open);
StreamReader fReader = new StreamReader(fStream);
string fLine = fReader.ReadLine();
string[] fContent = fLine.Split('|');
if (fContent[2].Equals(txtusername2.Text))
{
fContent = fLine.Split('|');
MessageBox.Show("username already exists!");
fLine = fReader.ReadLine();
fReader.Close();
fStream.Close();
}
else
{
fReader.Close();
fStream.Close();
if (string.IsNullOrEmpty(txtlastname.Text) || string.IsNullOrEmpty(txtfirstname.Text) || string.IsNullOrEmpty(txtusername2.Text) || string.IsNullOrEmpty(txtpassword2.Text) || string.IsNullOrEmpty(cmbaccounttype.Text))
{
MessageBox.Show("ERROR! There is an empty text.");
}
else
{
using (StreamWriter record = new StreamWriter("UserAccount.Txt", true))
{
record.WriteLine(txtlastname.Text + "|" + txtfirstname.Text + "|" + txtusername2.Text + "|" + txtpassword2.Text + "|" + cmbaccounttype.Text);
}
MessageBox.Show("Account successfully registered!");
cleanup();
}
}
}
I tried changing the format of my streamwriter, i'm not sure if that's the problem. I'm expecting that there would be no duplicate in the data of the .txt file and what the user inputs.
You only read the first line of the file and you have to read and compare all the lines using while ((s = sr.ReadLine()) != null):
bool _isUserExist = false;
using (StreamReader sr = File.OpenText("UserAccount.txt"))
{
string s = String.Empty;
while ((s = sr.ReadLine()) != null)
{
if (s.Split('|')[2] == txtusername2.Text)
{
_isUserExist = true;
MessageBox.Show("username already exists!");
break;
}
}
}
if(!_isUserExist)
{
if (string.IsNullOrEmpty(txtlastname.Text) || string.IsNullOrEmpty(txtfirstname.Text) || string.IsNullOrEmpty(txtusername2.Text) || string.IsNullOrEmpty(txtpassword2.Text) || string.IsNullOrEmpty(cmbaccounttype.Text))
{
MessageBox.Show("ERROR! There is an empty text.");
}
else
{
using (StreamWriter record = new StreamWriter("UserAccount.Txt", true))
{
record.WriteLine(txtlastname.Text + "|" + txtfirstname.Text + "|" + txtusername2.Text + "|" + txtpassword2.Text + "|" + cmbaccounttype.Text);
}
MessageBox.Show("Account successfully registered!");
cleanup();
}
}
I have an requirement to read the SGML file and replace if the symbol like comma(,) or full stop (.) then I need to change the symbols and save with the same SGML file itself but I am facing the format issue after replacing the content.
Below is my code and my final output would be store with the same .sgm format.
The below code is working but after replacing the values the output format is differs. Can you suggest for this scenario
Main method:
string resultValue = HTMLToEntity(ReplaceSGML(sbContent.ToString()));
StringReader sr = new StringReader(resultValue.ToString());
SgmlReader reader = new SgmlReader();
reader.WhitespaceHandling = WhitespaceHandling.All;
reader.CaseFolding = Sgml.CaseFolding.ToLower;
reader.InputStream = sr;
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.Formatting = System.Xml.Formatting.Indented;
w.WriteStartDocument();
reader.Read();
while (!reader.EOF)
{
w.WriteNode(reader, true);
}
//File.WriteAllText(#"C:\Output\test.sgm", );
w.Flush();
w.Close();
Method : ReplaceSGML
private static string ReplaceSGML(string html)
{
XmlDocument xml = new XmlDocument();
xml.Load(_xmlEnglishPath);
XmlNodeList resources = xml.SelectNodes("root/data");
_htmlEnglishDictonaries = new Dictionary<string, string>();
_htmlEnglishDictonaries.Add(";", "{After1Space}"); // replacing semicolon into {After1space}
_htmlEnglishDictonaries.Add(":", "{Before1Space}"); // replacing colon into {Before1Space}
_htmlEnglishDictonaries.Add(".", "{Before1Space}"); // replacing . into {Before1Space}
string line = string.Empty;
StringReader reader = new StringReader(html);
while (reader.Peek() > -1)
{
line = reader.ReadLine();
foreach (var events in _htmlEnglishDictonaries)
{
if (line.Contains(events.Key))
{
// Rule should be implement
// <!-- Replacetext 1.{After1Space}, 2.{Before1Space}, 3.{NoSpace}, 4. {After1LetterCaps} -->
int idx;
if (events.Value.ToLower().Trim() == "{after1space}")
{
idx = line.IndexOf(events.Key) + events.Key.Length;
if (line[idx].ToString() != " ")
{
line = line.Replace(events.Key, events.Key + " ");
}
}
if (events.Value.ToLower().Trim() == "{before1space}")
{
idx = line.IndexOf(events.Key);
if (line[idx].ToString() != " ")
{
line = line.Replace(events.Key, " " + events.Key);
}
}
if (events.Value.ToLower().Trim() == "{before1space},{after1space}")
{
idx = line.IndexOf(events.Key);
if (line[idx].ToString() != " ")
{
line = line.Replace(events.Key, " " + events.Key + " ");
}
}
if (events.Value.ToLower().Trim() == "{nospace}")
{
idx = line.IndexOf(events.Key);
if (line[idx].ToString() != " ")
{
line = line.Replace(events.Key, " " + events.Key);
}
}
if (events.Value.ToLower().Trim() == "{after1lettercaps}")
{
idx = line.IndexOf(events.Key) + events.Key.Length;
if (line[idx].ToString() != " ")
{
if (line[idx + 1].ToString() != " ")
{
line = line.Replace(events.Key, " " + events.Key + line[idx + 1].ToString().ToUpper());
}
else
{
line = line.Replace(events.Key, " " + events.Key);
}
}
}
}
}
}
return line.ToString();
}
Thanks in advance
when i run this code after 30 second give me error 'time out expire' . error comming exactly after fill dataset . by the way i add connection time out = 0 to my app config but still the problem is the same . so how i can set connection time out for this dataset
public void ExportInvoicesAccount()
{
var q = from h in hmsdb.TransHdrs
where h.Account.AccountsContracts.First().AccountsMain.ID == _mainaccid
&& h.PayMethod == "R"
&& h.CancelDate == null
&& h.TransDate >= _fromdate
&& h.TransDate <= _todate
group h by new
{
amID = h.Account.AccountsContracts.First().AccountsMain.ID,
amcode = h.Account.AccountsContracts.First().AccountsMain.Code,
amName = h.Account.AccountsContracts.First().AccountsMain.EngName,
acccode = h.AccountCode,
accid = h.AccountID,
accname = h.Account.EngName
} into qg
select new
{
amID = qg.Key.amID,
amCode = qg.Key.amcode,
amName = qg.Key.amName,
acccode = qg.Key.acccode,
accid = qg.Key.accid,
accname = qg.Key.accname
};
if (_facccode != "" && _taccccode == "")
{
q = q.Where(f => f.acccode == _facccode);
}
if (_facccode != "" && _taccccode != "")
{
q = q.Where(f => Convert.ToInt32(f.acccode) >= Convert.ToInt32(_facccode) && Convert.ToInt32(f.acccode) <= Convert.ToInt32(_taccccode) && f.acccode != "C" && f.acccode != "10281501مكرر ");
}
foreach (var x in q)
{
try
{
ClaimDS ds = new ClaimDS();
SP_EClaims_StatmentOfAccountGeneralTableAdapter adapt = new SP_EClaims_StatmentOfAccountGeneralTableAdapter();
ds.EnforceConstraints = false;
adapt.Fill(ds.SP_EClaims_StatmentOfAccountGeneral, x.amID, x.accid, 0, _fromdate, _todate, _inout,0,0,0, 0);
if (ds.SP_EClaims_StatmentOfAccountGeneral.Rows.Count != 0)
{
InvoicesByAcc rptinv = new InvoicesByAcc();
rptinv.SetDataSource(ds);
ExportOptions expop = new ExportOptions();
DiskFileDestinationOptions dfdo = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions pdfop = new PdfRtfWordFormatOptions();
FolderPath = _path + x.amCode + " - " + x.amName + "\\";
bool exists = System.IO.Directory.Exists(FolderPath);
if (!exists)
System.IO.Directory.CreateDirectory(FolderPath);
fpath = FolderPath;
rptinv.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
rptinv.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
DiskFileDestinationOptions objDiskOpt = new DiskFileDestinationOptions();
if (_inout == "O")
{
objDiskOpt.DiskFileName = FolderPath + "\\" + x.acccode + "-" + "0000" + "-0000" + "-" + "001" + "-OUT" + ".pdf";
}
else if (_inout == "I")
{
objDiskOpt.DiskFileName = FolderPath + "\\" + x.acccode + "-" + "0000" + "-0000" + "-" + "001" + "-IN" + ".pdf";
}
else
{
objDiskOpt.DiskFileName = FolderPath + "\\" + x.acccode + "-" + "0000" + "-0000" + "-" + "001" + "-ALL" + ".pdf";
}
rptinv.ExportOptions.DestinationOptions = objDiskOpt;
rptinv.Export();
rptinv.Dispose();
rptinv.Close();
}
GC.Collect();
ds.Dispose();
ds.Clear();
}
catch (Exception ex)
{
string logpath = FolderPath + "E_Claim_ErrorLog.txt";
// This text is added only once to the file.
if (!File.Exists(logpath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(logpath))
{
//sw.WriteLine(ex.Message + "( " + "AccountMainID: " + x.amID + " - " + "AccountID: " + x.accid + " - "+ "ConsID: " + x.consid + " - " + "MRN: " + x.mrn + " )");
}
}
// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(logpath))
{
sw.WriteLine(ex.Message + " SP_EClaims_StatmentOfAccountGeneral" + "ExportInvoicesAccount" + "( " + "AccountMainID: " + x.amID + " - " + "AccountID: " + x.accid + " - " + "ConsID: " + "0" + " - " + "MRN: " + "0" + " )");
}
//MessageBox.Show(ex.Message + "AccountMainID: " + x.amID + "-"+ "AccountID: " + x.accid + "ConsID: " + x.consid + "MRN: " + x.mrn );
}
}
}
For some reason my Else statement is always being executed even when the if statement is.
string line;
string[] columns = null;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split(',');
if (columns.Contains(tboxName.Text))
{
rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3]));
}
else
{
MessageBox.Show("No Hotels Found.");
break;
}
Is this because it is searching through every line in the file because of the while loop and not every line contains tboxName?
If so how would it be able to return all values of column[0] without using a while loop?
If I understand correctly, you want to show the message box if none of the lines in the file contain tboxName.Text? If so you can do this check after the while loop completes, using a bool to track whether any line did have a match:
string line;
string[] columns = null;
bool foundHotels = false;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split(',');
if (columns.Contains(tboxName.Text))
{
rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3]));
foundHotels = true;
}
}
if(!foundHotels)
{
MessageBox.Show("No Hotels Found.");
}
Try something like this
string[] columns = null;
var isHotels = false;
while ((line = sr.ReadLine()) != null)
{
columns = line.Split(',');
if (columns.Contains(tboxName.Text))
{
rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3]));
isHotels = true;
}
} // while loop ends
if (!isHotels)
{
MessageBox.Show("No Hotels Found.");
break;
}
How I could detect at application runtime
what version numbers of .NET framework are installed on the client machine?
Environment.Version.ToString()
private static void GetVersionFromRegistry()
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32).OpenSubKey(#"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
{
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", "");
string sp = versionKey.GetValue("SP", "").ToString();
string install = versionKey.GetValue("Install", "").ToString();
if (install == "") //no install info, ust be later
Console.WriteLine(versionKeyName + " " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
}
}
if (name != "")
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", "");
if (name != "")
sp = subKey.GetValue("SP", "").ToString();
install = subKey.GetValue("Install", "").ToString();
if (install == "") //no install info, ust be later
Console.WriteLine(versionKeyName + " " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
}
else if (install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name);
}
}
}
}
}
}
}
resources info :
http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx