|
I'm trying to pass a variable from a GridViewRowEvent method to a DataListItemEvent method in a C# program.
I have the following:
public void My_RowDataBound2(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int lblAmount = (int)DataBinder.Eval(e.Row.DataItem, "Amount");
totalAmount += lblAmount;
x += 1;
}
else
{
CountNumberRows(x);
}
}
public int CountNumberRows(int x) { return x; }
public void My_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.DataItem != null)
{
Label dummyVariable = (Label)e.Item.FindControl("abc");
abc.Text = CountNumberRows(x).ToString();
}
}
Then in my ASP.NET file I have the following entry:
DataList
...
Gridview
...
Gridview
...
<asp:Label ID="abc" runat="server"></asp>
...
DataList
And I get just "0" for the "abc" value in my file. I know the GridViewEvent method works because the variable
totalAmount correctly sums up all the data each time it is called by My_RowDataBound2, and inserted into the appropriate place in the GridView on my ASP.NET Page. But "abc" just shows 0. abc is just supposed to show the number of rows contained in the GridView.
Basically, I just need to get the "x" value from
My_RowDataBound2 method, pass it to the
My_ItemDataBound event which then passes it as a label to the ASP.NET Page. But the label is just showing "0"
If I take:
public int CountNumberRows(int x) { return x; } and change it to:
public int CountNumberRows(int x) { x= "123"; return x; } , it passes the "123" to the abc label. But I don't know how to pass the x calculation out of the GridView to the DataList. Any suggestions? I appreciate your help
|
|
|
|
|
Dear all,
How can i get the names of the directories of a ftp server in c#?
Thanks.
|
|
|
|
|
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + server + "/"));
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi Mark,
Thank you for reply, but with this code you will get the file names.
What i need is the folder names.
|
|
|
|
|
You have what you need to research for yourself
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
In C# app i need to check if a file is a text file or is a binary file,
how can i do this ?
i can't check by file extension, because isn't meaningful for me .
|
|
|
|
|
|
i've found a solutions, if can be useful for someone else :
static bool IsTextFile(string fileName)
{
byte[] file;
using (System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
file = new byte[stream.Length];
stream.Read(file, 0, file.Length);
}
if (file.Length > 3 && ((file[0] == 0x00 && file[1] == 0x00 && file[2] == 0xFE && file[2] == 0xFF /*UCS-4*/)))
return true;
else if (file.Length > 2 && ((file[0] == 0xEF && file[1] == 0xBB && file[2] == 0xBF /*UTF-8*/)))
return true;
else if (file.Length > 1 && ((file[0] == 0xFF && file[1] == 0xFE /*Unicode*/)))
return true;
else if (file.Length > 1 && (file[0] == 0xFE && file[1] == 0xFF /*Unicode Big Endian*/))
return true;
else
{
for (int i = 0; i < file.Length; i++)
if (file[i] > 0x80)
return false;
return true;
}
}
Bye
|
|
|
|
|
Well you know, that is really not correct - or rather, too simplistic. UTF-8 doesn't require a byte order mark, for starters, and non-text can very easily start with an accidental byte order mark (since it is so short), you should really write a more complex test. I'll see what I can come up with, I may get back to you.
Edit: how about this?
static bool IsText(byte[] data)
{
if (data.Length < 16)
throw new Exception("Data is too short to guess the encoding");
byte asciitester = 0;
for (int i = 0; i < data.Length; i++)
asciitester |= data[i];
if (asciitester < 0x80)
return true;
if (IsValidUTF8(data))
return true;
if (IsValidUTF16(data))
return true;
return false;
}
static bool IsValidUTF16(byte[] data)
{
if ((data.Length & 1) == 1)
return false;
bool LittleEndian;
ushort BOM = BitConverter.ToUInt16(data, 0);
if (BOM == 0xFFFE)
LittleEndian = false;
else if (BOM == 0xFEFF)
LittleEndian = true;
else
return false;
bool SecondOfSurrogatePair = false;
for (int i = 2; i < data.Length; i+=2)
{
int code;
if (LittleEndian)
code = data[i] | (data[i + 1] << 8);
else
code = (data[i] << 8) | data[i + 1];
if (SecondOfSurrogatePair)
{
if (code < 0xDC00 || code > 0xDFFF)
return false;
SecondOfSurrogatePair = false;
}
else
{
if (code >= 0xD800 && code <= 0xDBFF)
SecondOfSurrogatePair = true;
}
}
return SecondOfSurrogatePair;
}
static bool IsValidUTF8(byte[] data)
{
int index = 0;
if (data[0] == 0xEF)
{
if (data[1] != 0xBB ||
data[2] != 0xBF)
return false;
index = 3;
}
int mode = 0;
for (int i = index; i < data.Length; i++)
{
switch (mode)
{
case 0:
if (data[i] < 0x80)
break;
else if (data[i] < 0xC0)
return false;
else if (data[i] < 0xE0)
mode = 1;
else if (data[i] < 0xF0)
mode = 2;
else if (data[i] < 0xF5)
mode = 3;
else
return false;
break;
case 1:
case 2:
case 3:
if (data[i] < 0x80 || data[i] >= 0xC0)
return false;
mode--;
break;
default:
return false;
}
}
return mode == 0;
}
I suppose you could tests to see if there are any unprintable characters, too.
modified on Monday, April 5, 2010 2:02 PM
|
|
|
|
|
Btw, could I get some feedback on code this, please? Specifically on whether the result is correct as far as it tests, this test is "supposed to" be very optimistic in what it accepts as text, so I'm especially interested in whether there are valid UTF-16 or UTF-8 strings that get rejected by the test. I'm not exactly a Unicode expert..
|
|
|
|
|
This isn't going to work in all cases since there is no requirement for a text file to have byte order marks. Also, what if the binary file just happens to have those very bytes in the place where you would normally have byte order marks?
What do you do if there are NO byte order marks (which is most text files anyway...)?
|
|
|
|
|
Hey, what happened to DOS/Windows marking the End Of File with Ctrl+Z (26) when it's a text file?
|
|
|
|
|
That character never made it into the text file. I only ever saw it used when copying from CON to a file. Ctrl-Z just denoted the end of the stream that was being typed.
|
|
|
|
|
Text mode reading wouldn't return it, but I used to see it when reading text files in binary mode.
|
|
|
|
|
That is not in general possible, you can at best make a "pretty good guess".
For example you can first check whether the file is a valid UTF-8 string, if the file is sufficiently big the chance is very small that a non-text file is accidentally valid UTF-8.
But you can't be quite sure, an invalid UTF-8 string could be made invalid because it is slightly corrupted or badly encoded and still "be"* text, and a binary file could accidentally (although the chance is quite small) be valid UTF-8.
Btw, ASCII files will validate as valid UTF-8.
And you could add some other things, such as checking for a byte order mark and/or UTF-16
* let's not argue about the meaning of "being a text file", but there are several ways to look at it, for example "if it can be decoded to text, it is a text file regardless of whether the text is meaningful" or "if and only if it was originally written as a text file, it is a text file"
If you know the text is going to be in a particular language you have some more options, such as checking the statistical properties of the text against expected values based on the language - that is still no sure-fire way to know whether something is actually text or even to reject all non-text, though.
No matter what you do with the result of the "text test", it had better be robust enough to handle "accidental non-text", and having a "I am sure this is text, do it anyway" option may be useful too.
|
|
|
|
|
I think if I ever need to create a binary file format, I'll be sure to start it with 0xFFFE.
harold aptroot wrote: "if and only if it was originally written as a text file, it is a text file"
That's my point of view.
I still want to know why the original poster wants to do this.
|
|
|
|
|
Win!
For extra points, make the rest of the file valid UTF-16
PIEBALDconsult wrote:
I still want to know why the original poster wants to do this.
Yea me too, I always get curious when people want to do things that are theoretically impossible. Which this is. I'd also like to know whether he's going to put in his own, rather brittle, test.. and I'll admit that my own code isn't that much better
|
|
|
|
|
Read in the file assuming it is text. Check for common words, like "is", "it", "or", "the", "that", "they", or any word you might expect to be in the text file (the bigger the smallest word you check for, the more accurate the check will be). Then check the text to ensure it only contains the characters that you can type on a keyboard (numbers, letters, punctuation). That should give you an ok guess as to whether or not it is a text file (which contains english text), without causing you too much work.
|
|
|
|
|
Hi all,
I just found out that there are many .lnk parser in C++/C/Java, but I am wondering if there is any reference code on this topic for C#.
Thanks a lot!
|
|
|
|
|
I use .Net version 2.0. I recently implemented double buffering in an animation-heavy aspect of my application--instead of drawing directly to the screen I copy the screen to a Bitmap, use a graphics object to draw on the bitmap, and copy the bitmap back to the screen. It has made the animation faster and smoother. The code looks like this:
Bitmap offScreenBmp;
Graphics offScreenDC;
offScreenBmp = new Bitmap(this.Width, this.Height);
offScreenDC = Graphics.FromImage(offScreenBmp);
// I do drawing in offScreenDC
// I do drawing in offScreenDC
// I do drawing in offScreenDC
Graphics clientDC = this.CreateGraphics();
clientDC.DrawImage(offScreenBmp, 0, 0);
It works perfectly in most situations. However, if I use remote desktop to access a computer on which my application is running (which I often do) I notice that there is this awful white flicker that happens every time I copy my bitmap to the screen. The flicker occurs wherever my bitmap has been changed by its Graphics, and I assume it's some sort of clearing the screen action that happens before every copy, and it just happens too quickly for me to see when I'm not using RDP.
Does anyone know how to avoid this or turn it off? I would really like to keep using this new technique.
|
|
|
|
|
Basically i was told to write a file watcher.
what i need is a configuration files that edit the watch path inside the .exe application?
Get the previous time and date this application had run.
This is the code that i haven wrote so far. Also asking if anyone is willingly to be my personal mentor in coding.
<br />
public partial class frmNotifier : Form<br />
{<br />
private StringBuilder m_Sb;<br />
private bool m_bDirty;<br />
private System.IO.FileSystemWatcher m_Watcher;<br />
private bool m_bIsWatching;<br />
<br />
<br />
public frmNotifier()<br />
{<br />
InitializeComponent();<br />
m_Sb = new StringBuilder();<br />
m_bDirty = false;<br />
m_bIsWatching = false;<br />
<br />
<br />
<br />
<br />
RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);<br />
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());<br />
<br />
}<br />
<br />
private void frmNotifier_Load(object sender, EventArgs e)<br />
{<br />
string date,time;<br />
date =DateTime.Now.ToString("dd/MM/yyyy");<br />
time = DateTime.Now.ToString("hh:mm:sstt");<br />
<br />
lstNotification.Items.Add("Program executed at: " + date+" , " + time);<br />
<br />
m_bIsWatching = true;<br />
m_Watcher = new System.IO.FileSystemWatcher();<br />
m_Watcher.Filter = "*.*";<br />
m_Watcher.Path = "D:\\TEMP\\";<br />
m_Watcher.IncludeSubdirectories = true;<br />
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite<br />
| NotifyFilters.FileName | NotifyFilters.DirectoryName;<br />
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);<br />
m_Watcher.Created += new FileSystemEventHandler(OnChanged);<br />
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);<br />
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);<br />
m_Watcher.EnableRaisingEvents = true;<br />
<br />
}<br />
<br />
<br />
private void OnChanged(object sender, FileSystemEventArgs e)<br />
{<br />
<br />
if (!m_bDirty)<br />
{<br />
m_Sb.Remove(0, m_Sb.Length);<br />
m_Sb.Append(e.FullPath);<br />
m_Sb.Append(" ");<br />
m_Sb.Append(e.ChangeType.ToString());<br />
m_Sb.Append(" ");<br />
m_Sb.Append(DateTime.Now.ToString());<br />
m_bDirty = true;<br />
}<br />
}<br />
<br />
private void OnRenamed(object sender, RenamedEventArgs e)<br />
{<br />
if (!m_bDirty)<br />
{<br />
m_Sb.Remove(0, m_Sb.Length);<br />
m_Sb.Append(e.OldFullPath);<br />
m_Sb.Append(" ");<br />
m_Sb.Append(e.ChangeType.ToString());<br />
m_Sb.Append(" ");<br />
m_Sb.Append("to ");<br />
m_Sb.Append(e.Name);<br />
m_Sb.Append(" ");<br />
m_Sb.Append(DateTime.Now.ToString());<br />
m_bDirty = true;<br />
<br />
}<br />
}<br />
<br />
private void tmrEditNotify_Tick(object sender, EventArgs e)<br />
{<br />
if (m_bDirty)<br />
{<br />
lstNotification.BeginUpdate();<br />
lstNotification.Items.Add(m_Sb.ToString());<br />
lstNotification.EndUpdate();<br />
m_bDirty = false;<br />
}<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
private void frmNotifier_Resize(object sender, EventArgs e)<br />
{<br />
if (this.WindowState == FormWindowState.Minimized)<br />
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
FileChangeWatcher.Visible = true;
Thread.Sleep(1000);
}<br />
<br />
}<br />
}<br />
<br />
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)<br />
{<br />
Show();
this.WindowState = FormWindowState.Normal;
FileChangeWatcher.Visible = false;
}<br />
<br />
private void frmNotifier_FormClosing(object sender, FormClosingEventArgs e)<br />
{<br />
m_bIsWatching = false;<br />
m_Watcher.EnableRaisingEvents = false;<br />
m_Watcher.Dispose();<br />
<br />
string date, time,date1,time1;<br />
<br />
date =DateTime.Now.ToString("ddMMyyyy");<br />
time = DateTime.Now.ToString("hhmm tt");<br />
<br />
if (Directory.Exists("c:\\FileChangeLog"))<br />
{<br />
<br />
<br />
<br />
}<br />
else {<br />
Directory.CreateDirectory("C:\\FileChangeLog");<br />
}<br />
<br />
<br />
StreamWriter sw = new StreamWriter(@"c:\FileChangeLog\" + date + "@" + time + "log.txt");<br />
<br />
date1 = DateTime.Now.ToString("dd/MM/yyyy");<br />
time1 = DateTime.Now.ToString("hh:mm:ss:tt");<br />
lstNotification.Items.Add("Program ended at: " + date1 + " , " + time1);<br />
<br />
foreach (string sItem in lstNotification.Items)<br />
{<br />
sw.WriteLine(sItem);<br />
}<br />
sw.Close();<br />
}<br />
|
|
|
|
|
1) Read the bit at the top of the page: "How to get an answer".
2) Don't post your entire app - no-one is going to read it.
3) If you post code, use the "code block" widget (or <pre> </pre> codes) to preserve the formatting. The difference is:
private void frmNotifier_FormClosing(object sender, FormClosingEventArgs e)<br />
{<br />
m_bIsWatching = false;<br />
m_Watcher.EnableRaisingEvents = false;<br />
m_Watcher.Dispose();<br />
<br />
string date, time,date1,time1;<br />
<br />
date =DateTime.Now.ToString("ddMMyyyy");<br />
time = DateTime.Now.ToString("hhmm tt");<br />
<br />
if (Directory.Exists("c:\\FileChangeLog"))<br />
{<br />
<br />
<br />
<br />
}<br />
else {<br />
Directory.CreateDirectory("C:\\FileChangeLog");<br />
}<br />
<br />
<br />
StreamWriter sw = new StreamWriter(@"c:\FileChangeLog\" + date + "@" + time + "log.txt");<br />
<br />
date1 = DateTime.Now.ToString("dd/MM/yyyy");<br />
time1 = DateTime.Now.ToString("hh:mm:ss:tt");<br />
lstNotification.Items.Add("Program ended at: " + date1 + " , " + time1);<br />
<br />
foreach (string sItem in lstNotification.Items)<br />
{<br />
sw.WriteLine(sItem);<br />
}<br />
sw.Close();<br />
} and
private void frmNotifier_FormClosing(object sender, FormClosingEventArgs e)
{
m_bIsWatching = false;
m_Watcher.EnableRaisingEvents = false;
m_Watcher.Dispose();
string date, time,date1,time1;
date = DateTime.Now.ToString("ddMMyyyy");
time = DateTime.Now.ToString("hhmm tt");
if (Directory.Exists("c:\\FileChangeLog"))
{
}
else
{
Directory.CreateDirectory("C:\\FileChangeLog");
}
StreamWriter sw = new StreamWriter(@"c:\FileChangeLog\" + date + "@" + time + "log.txt");
date1 = DateTime.Now.ToString("dd/MM/yyyy");
time1 = DateTime.Now.ToString("hh:mm:ss:tt");
lstNotification.Items.Add("Program ended at: " + date1 + " , " + time1);
foreach (string sItem in lstNotification.Items)
{
sw.WriteLine(sItem);
}
sw.Close();
} Which would you rather read?
4) Be specific. What is your problem, not "some-one write it for me".
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
The esteemed John Simmons has written a great FileSystemWatcher class. Read his article here[^].
Don't blame me. I voted for Chuck Norris.
|
|
|
|
|
Hi,
I need to write an automated C# code to send the emails to the distribution list of an exchange server .
I have tried with the SmtpClient class.But it was giving an error saying "Failure sending mail."
Plz help me on this,
Thanks in Advance,
Ravi.
|
|
|
|
|
See here.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|