|
Hi! Im creating a form in which i retrieve data from a database and attach it to a grid. I need to have a function in which if i click the save button the results of the retrieved data will be saved as a "text" file. cane someone please tell me what's wrong with my code bcoz whenever i click my save button it doesn't save at all. here's my code:
private void btnSave_Click(object sender, System.EventArgs e)
{
OdbcConnection cnn = new OdbcConnection();
cnn.Open();
OdbcCommand cmmd = new OdbcCommand();
OdbcDataAdapter adp = new OdbcDataAdapter (cmmd);
DataSet ds_emp = new DataSet();
adp.Fill(ds_emp);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0;i<=ds_emp.Rows.Count - 1; i++)
{
for(int q=0;q<=ds_emp.Columns.Count - 1; q++)
{
sb.Append(ds_emp.Rows[i][q].ToString());
}
sb.Append(" ");
}
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=*.txt");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.text";
System.IO.StringWriter strWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(strWrite);
Response.Write(sb.ToString());
}
where ds_emp is the retrived data. Is there something wrong with my code? It builds but it doesn't work. Thanx for all your replies! I appreciate it
thanx
|
|
|
|
|
I have the following URL:
http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=/netahtml/search-adv.htm&r=2&p=1&f=G&l=50&d=ptxt&S1=((teeth+OR+member)+OR+provide)&OS=+(teeth+OR+member)+OR+provide&RS=((teeth+OR+member)+OR+provide)
Accessing it with the following code snippet (which works for other sites):
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
throws a WebException (timeout). Accessing the same URL with IE works fine. I compared the request header sent by the code and the one sent by IE and they are identical. The user agent has been set to "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50215)"
I even increased the request timeout to a massively huge value and it didn't make any difference.
|
|
|
|
|
There is something wrong with the headers. I was getting this error: "The underlying connection was closed: The server committed an HTTP protocol violation." put this in your config file and it should resolve the issue.
<configuration>
<system.net>
<settings>
<httpwebrequest useunsafeheaderparsing="true">
|
|
|
|
|
Put what? It looks like you forgot to check the box below the message window that says "Do not treat <<s as HTML tags".
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Sorry dude. Here it is:
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
|
|
|
|
|
You might want to reply to the original poster with this, not me. He'll get a notification e-mail that says you replied to him that way.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Is it possible to insert a jpg,gif or a bitmap file into a crystal report programmatically? If so can someone point me in the right direction. I've been looking on the web but can't seem to find anything about it.
|
|
|
|
|
Hello!
I have an old notebook with no display (don’t ask - long story). Now I want to put it back in use by developing a Text-to-Speech-based menu, e.g. I press ‘m’ and the program outputs something like “You are in the main menu. Press 1 to do …., press 2 to do ….., …” via TTS – all just for fun. The TTS part isn’t much of a problem but I was wondering if I could reassign keys like the windows key. Essentially, I would like to globally (before Windows does) intercept any keyboard events and then pass them directly to an application of my choice or simply drop them.
Am I looking for a hook? Or do they, as I’ve read, only allow for monitoring system events but not altering or discarding them all together?
If I’m wrong with that assumption I would appreciate any help concerning hooks and especially their interop side. I’ve read that global hooks require the code to reside in a DLL? I guess this doesn’t include managed DLLs and I would have to do it in unmanaged C++.
If I’m right I’m open to any suggestions and any useful resources you can provide.
Thanks in advance.
Regards,
Florian
|
|
|
|
|
I've done something kind of similar to what your thinking of. I did it in C++ though. I found it much easier than trying to Interop the thing. You'll also be able to rebuild/customize keyboard messages easier too. If you want to eat a keyboard message, it's easy enough. You just don't pass it up the hook chain when you're done processing it, if any.
Global, low-level hooks, can be in an .EXE and still work. The latest example I used of this was to eat any Windows key message. (This effectively disables hitting Windows-E and starting Explorer, even though Group Policy disabled running it!)
You're only problem, that I see, is going to be maintaining control of focus while you send keyboard messages around.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thank you so far.
I haven't been programming C++ for 2 years now so I would prefer doing as much as possible in C#.
I guess I could write a C++ DLL and then wrapp it with some managed code.
I think I'm going to read a few MSDN articles about hooks.
|
|
|
|
|
I didn't say it couldn't be done entirely in C#, I just said I found it easier to do in C++. If you wanted to, you could write the thing in Managed C++ so it's easier to build an interface to your C# app.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I found an interesting article on .NET 247 (http://www.dotnet247.com/247reference/msgs/26/134226.aspx) and rewrote the code to match the new C++ syntax introduced with .NET 2.0.
#pragma once
#include <windows.h>
#undef MessageBox
#using <system.dll>
#using <system.windows.forms.dll>
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
#pragma data_seg(".Share")
HHOOK g_Hook = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.Share,rws")
namespace Hooks
{
int __stdcall KeyboardProc(int code, int wparam, int lparam)
{
String^ text = String::Concat("You pressed : ", ((Keys)wparam).ToString());
MessageBox::Show(text, "Hook Message", MessageBoxButtons::OK);
return CallNextHookEx(g_Hook, code, wparam, lparam);
return 0;
}
public ref class KeyboardHook
{
private:
KeyboardHook() {};
public:
static void SetHook() {
if (g_Hook != 0)
return;
Module^ mod = Assembly::GetExecutingAssembly()->GetModule("Hooks.dll");
IntPtr hinst = Marshal::GetHINSTANCE(mod);
g_Hook = SetWindowsHookEx(WH_KEYBOARD, reinterpret_cast<HOOKPROC>(KeyboardProc), static_cast<HINSTANCE>(hinst.ToPointer()), 0);
}
static void Unhook() {
if (g_Hook != 0) {
UnhookWindowsHookEx(g_Hook);
g_Hook = 0;
}
}
};
};
At first I don’t understand exactly why the g_Hook variable is declared after #pragma data_seg(“.Share”).
Besides this I need a more flexible way to integrate it into C# so I declared a managed class KeyboardHookEventArgs and a delegate KeyboardHookDelegate which I wanted to pass in from C#.
The delegate should then be called and pass all information about the keyboard event in my C# app.
The problem is I can’t invoke the delegate from inside the HOOKPROC callback because the delegate is managed but the callback is not (__stdcall).
Another issue: How do I check that the callback variable is assigned? In C# I would check if the variable is null but in C++, using NULL, I get the error that my delegate type can’t be implicitly casted to int.
Here the code for the KeyboardHookEventArgs and the delegate:
public ref class KeyboardHookEventArgs
{
private:
bool block;
int code;
int wParam;
int lParam;
public:
property bool Block
{
bool get()
{
return block;
}
void set(bool value)
{
block = value;
}
}
property int Code
{
int get()
{
return code;
}
void set(int value)
{
code = value;
}
}
property int WParam
{
int get()
{
return wParam;
}
void set(int value)
{
wParam = value;
}
}
property int LParam
{
int get()
{
return lParam;
}
void set(int value)
{
lParam = value;
}
}
KeyboardHookEventArgs()
{
block = false;
code = 0;
wParam = 0;
lParam = 0;
}
KeyboardHookEventArgs(int code, int wParam, int lParam)
{
this->code = code;
this->wParam = wParam;
this->lParam = lParam;
this->block = false;
}
KeyboardHookEventArgs(int code, int wParam, int lParam, bool block)
{
this->code = code;
this->wParam = wParam;
this->lParam = lParam;
this->block = block;
}
};
public delegate void KeyboardHookDelegate(KeyboardHookEventArgs^ e);
The Block property in the event args is used to determine wether the event should or should not be passed on.
I’m really stuck here and hope somebody can give me some hints.
|
|
|
|
|
|
Ok i need to know how to figure out how many milliseconds have passed over a period of time. I do this with two... "time stamps" - dtmCurrent & dtmPrevious - I then just subtract (dtmCurrent-dtmPrevious) and i get a TimeSpan. How do i convert that TimeSpan into an int? I need that int to be how many milliseconds have passed in that TimeSpan, though.
Of course if there is a better way to do this... without a timer... then i'm all up for suggestions.
Here's a snipet of my code thats used.
bool FirstRun = true;<br />
DateTime dtmCurrent;<br />
DateTime dtmPrevious;<br />
<br />
private void MainGame()<br />
{<br />
if (FirstRun == true)<br />
{<br />
this.ObjectEngine.Create_Unit(1,0,1,400,400,0);<br />
FirstRun = false;<br />
dtmCurrent = DateTime.Now;<br />
}<br />
dtmPrevious = dtmCurrent;<br />
dtmCurrent = DateTime.Now;<br />
<br />
for (int i=0; i<ObjectEngine.ActiveUnitQPt; i++)<br />
{<br />
Sprite_Update((dtmCurrent - dtmPrevious), i);<br />
}<br />
SpriteEngine.Draw();<br />
}
|
|
|
|
|
|
Hi..
I am getting this Error while DateTime.Parse(txtActStartDate.ToString())
where the textbox contains - 31-01-2004 -- in dd-MM-yyyy format
The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
Whats the problem?
Thanks
|
|
|
|
|
Please use the overloaded Parse method
DateTime.Parse Method (String, IFormatProvider, DateTimeStyles)
and specify the Format.
|
|
|
|
|
I used .. Convert.ToDateTime("31/01/2004",new CultureInfo("en-GB"))
But still getting the same error, I want to specify British Date Style..
???
Thanks
|
|
|
|
|
It works just fine when I try it.
Does your code look exactly like that?
---
b { font-weight: normal; }
|
|
|
|
|
Hi,
I have set the date value in the text box. I am trying to save in this way...
Convert.ToDateTime(txtname.Text,new CultureInfo("en-GB")).. txtname contains 31/03/2004
This is throwing the same error.
But again I hard-code the value of txtname.text with
Convert.ToDateTime("31/03/2004",new CultureInfo("en-GB"))
Its working fine..
What cud be the problem with the textbox?
Thanks
|
|
|
|
|
Have you tried to write out the value from the text box, so that you are sure that it is sent to the server correctly?
---
b { font-weight: normal; }
|
|
|
|
|
That's because your using the Convert class. Use the DateTime Parse method to do this:
DateTime newDate = DateTime.Parse("31/01/2004", new CultureInfo("en-GB"));
or
DateTime newDate = DateTime.Parse("31/01/2004", "dd/MM/yyyy", Application.CurrentCulture.DateTimeFormat);
also works.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks a lot, its working fine..
But wats the diff between Convert and DateTime class for this. It was working with a hardcoded string and not with a textbox?
|
|
|
|
|
When you're converting a string to any kind of object, if the object has a Parse method, use it. IMHO, it's always going to work better than the more generic methods in Convert. Also, Parse deals with whitespace and invalid character issues better. I've almost gotten away with never using Convert in any of my apps...
There is no difference between a TextBox's Text property and a String. The Text property returns a String object, so, the only possible problem is an input error in the TextBox. If you include leading and/or trailing spaces, tabs, incorrect formatting characters, ..., Convert might not work whereas Parse might.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Please stop double posting.
I just answered this question in the asp.net section.
---
b { font-weight: normal; }
|
|
|
|