|
Please edit your question, and use the "Code block" widget to preserve the formatting. In addition, tick the checkbox marked 'Encode "<" (and other HTML) characters when pasting' and re paste your sample code. At present, your code cannot be fully read as the HTML is swallowing the for loop condition!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
Hi,
I am having problem in displaying report in doc file. the code is
SqlConnection cnx = new SqlConnection(categories.strCnx);
cnx.Open();
string query = @"select * from categories";
SqlCommand cmd = cnx.CreateCommand();
cmd.CommandText = query;
SqlDataReader dr;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=categories.doc");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-document";
HttpContext.Current.Response.Write("categories" + '\n');
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
s[i]= dr[0].tostring();
i++;
}
for (int j = 0;j < i;j++)
{
HttpContext.Current.Response.Write(s[j].ToString() + '\n');
}
HttpContext.Current.Response.End();
}
catch(Exception ex)
{
string pt = ex.ToString();
}
In this code, if I write the file inside the while(dr.read()), it works fine but when i tried to write it inside the for loop, it doesn't give the output.
Why is that and how to solve it ?? Any idea ??
suchita
|
|
|
|
|
No, not quite.
Place the "code block" around your code, not your existing message. It does this:
while (dr.Read())
{
s[i]= dr[0].tostring();
i++;
}
And use the "Encode" option so that the HTML doesn't swallow < and > characters!
for (int j = 0; j Is not as helpful as
for (int j = 0; j < i - 17; j+= 123) Use the preview button to see what is happening.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
Thank you. I never used code block before. anyway it makes easy to read the code.
SqlConnection cnx = new SqlConnection(categories.strCnx);
cnx.Open();
string query = @"select * from categories";
SqlCommand cmd = cnx.CreateCommand();
cmd.CommandText = query;
SqlDataReader dr;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=categories.doc");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-document";
HttpContext.Current.Response.Write("categories" + '\n');
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
s[i]= dr[0].tostring();
i++;
}
for (int j = 0;j<i;j++)
{
HttpContext.Current.Response.Write(s[j].ToString() + '\n');
}
HttpContext.Current.Response.End();
}
catch(Exception ex)
{
string pt = ex.ToString();
}
Thanks
suchita
|
|
|
|
|
Firstly, change your code so it compiles - dr[0].tostring() won't - it needs to be ToString().
Then I suspect it fails and throws an exception for some reason. Start by putting a breakpoint on both the
dr = cmd.ExecuteReader(); and
string pt = ex.ToString(); lines. Run your program. when you hit the first breakpoint, single step the code through - I suspect that at least one of the dr.Read elements is throwing an exception.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
protected void filetesting()
{
SqlConnection cnx = new SqlConnection(categories.strCnx);
cnx.Open();
string query = @"select * from categories";
SqlCommand cmd = cnx.CreateCommand();
cmd.CommandText = query;
SqlDataReader dr;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=categories.doc");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-document";
HttpContext.Current.Response.Write("categories" + '\n');
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
HttpContext.Current.Response.Write( dr[0].ToString());
i++;
}
for (int j = 0; j < i; j++)
{
temp += s[j]+ '\n';
}
HttpContext.Current.Response.End();
}
catch(Exception ex)
{
string pt = ex.ToString();
}
}
This works fine when i put the "HttpContext.Current.Response.End()" before the for loop but I want to run it after for loop. I want to execute "HttpContext.Current.Response.Write" command inside the for loop. But since it wasn't running, I kept it inside the while(dr.read()) code. But i really want to have "HttpContext.Current.Response.Write" inside the for loop.
suchita
|
|
|
|
|
OK, this won't fix your problem (though it may appear to in a way). Alter your try block to include a finally block as well, and move the Response.End to there:
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
HttpContext.Current.Response.Write( dr[0].ToString());
i++;
}
for (int j = 0; j < i; j++)
{
temp += s[j]+ '\n';
}
}
catch(Exception ex)
{
string pt = ex.ToString();
}
finally
{
HttpContext.Current.Response.End();
}
What this does is ensure that whatever you have written to the HttpContext will get written to your client, whether it fails or not.
THIS DOES NOT FIX THE PROBLEM!
You should find that some of your data is written, and some is not, which should give you a clue as to what is going wrong.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
When i try to store the datareader value in string array and using that string array to write in a file, it gave the same problem ?? Code is like below ::
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
s[i] = dr[0].ToString();
i++;
}
for (int j = 0; j < i; j++)
{
HttpContext.Current.Response.Write(s[j] + '\n';
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
string pt = ex.ToString();
}
finally
{ HttpContext.Current.Response.End(); }
suchita
|
|
|
|
|
Then the problem must be the string you get back from the DataReader - which implies a problem in your DB. Try checking for nulls in your for loop:
for (int j = 0; j < i; j++)
{
if (s[j] == null)
{
HttpContext.Current.Response.Write("HELP!\n");
}
else
{
HttpContext.Current.Response.Write(s[j] + "\n");
}
} If you get "HELP!" in your HttpContext, look at your data!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
the error was same as always.. no change in error.
System.NullReferenceException: Object reference not set to an instance of an object.
at catagories.Default_View.filetesting() in C:\Documents and Settings\smanandhar\My Documents\Visual Studio 2008\Projects\catagories\Default_View.ascx.cs:line 1224
Also if it was the database error, then it wouldn't be showing when i display the same data in datagrid. Well I can show them in datagrid without any error.
suchita
|
|
|
|
|
Post the actual code you are using (cut and paste rather than hand modify the existing post).
Since you say (for the first time) it is a null-reference error at line 1224 in your code: look at line 1224 in your code. I am pretty sure you will find it is the line:
HttpContext.Current.Response.Write(s[j] + "\n");
(Since there are 1224 lines, just post the single method - but make sure it comes from the VS source, and don't forget the "encode HTML" option when you paste it).
SayamiSuchi wrote: Also if it was the database error, then it wouldn't be showing when i display the same data in datagrid. Well I can show them in datagrid without any error.
What on earth makes you believe that?
All that means is that the datagrid takes care to check for null entries and display them as blank!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
It is showing the error is at line
s[i] = dr[0].ToString(); . The code
HttpContext.Current.Response.Write("HELP!\n");
also not working. Well I am using '\n' tab for new line. If i put "\n" , it will just print as "\n" and so i tried 'HELP!\n', which also didn't work. The code i was using is shown below :
protected void filetesting()
{
SqlConnection cnx = new SqlConnection(categories.strCnx);
string query = @"select i.Name,i.Type,j.Item,j.ItemDescription from categories i, items j where i.ID = j.ID";
SqlCommand objCmd = new SqlCommand(query, cnx);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=categories.doc");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-document";
SqlDataReader dr;
cnx.Open();
SqlCommand cmd = cnx.CreateCommand();
cmd.CommandText = query;
try
{
i = 0;
dr = cmd.ExecuteReader();
while (dr.Read())
{
s[i] = dr[0].ToString();
i++;
}
for (int j = 0; j < i; j++)
{
if (s[j] == null)
{
HttpContext.Current.Response.Write("HELP!\n");
}
else
{
HttpContext.Current.Response.Write(s[j] + '\n');
}
}
HttpContext.Current.Response.End();
}
catch (Exception ex)
{
string pt = ex.ToString();
}
finally
{ HttpContext.Current.Response.End(); }
}
Is there any other way to show the user formatted data in word document. Well if grab all my requirements in datagrid, how do I show them in word document. I guess the way I'm using is not correct..thats why I am getting these problems.
suchita
|
|
|
|
|
There are pretty much only two errors you can generate easily from
s[i] = dr[0].ToString(); One is an access violation, and implies that the string array "s" is not big enough to hold the data.
The other is a null reference exception, which implies that dr[0] returns a null.
Since you have said it is a null reference error you are getting, it must be dr[0] is null. Add a check for this in your code, and do not process that line any further:
if (dr[0] != null)
{
s[i] = dr[0].ToString();
}
What are you actually trying to achieve? Is this winforms, or ASP? If you are writing to a Word document, why are you using httpContext? Why not use Interop?
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
But I do not think dr[0] is null because if i put the following code, it displays the data.
while (dr.Read())
{
HttpContext.Current.Response.Write(dr[0]);
}
so I dont know where the error is ?
suchita
|
|
|
|
|
Well I tried to use another way. I showed it first to the datagrid and after i try to show it in word file. well that one works well. But here my problem is since I am showing them in datagrid, when it comes in word document, it shows the same way as in datagrid. But I want to have some formatting on it. Like for example, i want some field of the datagrid on the top where as somefield on the other side. Can I be able to do that in word document ?
suchita
modified on Thursday, July 22, 2010 10:28 AM
|
|
|
|
|
I'm not sure if I can get the answer on this...
I'm trying to find out of Visual Studio 2010 has the line to show location of open/closing bracket as seen on the photo... (this was from NotePad ++)
This will help me to see better where the open/close bracket belongings, rather than click on the closing bracket and look for the highlighted opening bracket.
Oh, speaking of photo, how do I attach the pix on this?
the line on opening/closing bracket can be seen on Notepad++ software.
I'll do the ascii text here... <sigh>
for (int i = 0; i < 5; i++)
{
| if (i == 2)
| {
| | ....
| | ....
| }
| else
| <throw exception>
}
|
|
|
|
|
I personally use end of brace comments. Also, if a method is larger than the screen, it is too large. With this rule of thumb and a preference for a lot of little methods deep nesting of loops can be avoided. As for getting visual studio to do what you want, good luck. It can barely manage MS preferences
for (..){
;
}
|
|
|
|
|
It is certainly available for VS 2008 as part of the MS extension CodeRush[^], but I don't know if that will work in VS 2010 (I'm waiting for SP1 before I think about upgrading). Worth a try though.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
Even though I use that brace-placement format, that's one of the things I don't like about Notepad++
It's not as flexible as VS' way of highlighting matching items.
|
|
|
|
|
0I have this date in xml web services..
7/19/10 5:00 PM Local Time
how can I convert it to:
Mon 19 Jul 2010 Local Time
|
|
|
|
|
Try:
DateTime d = DateTime(2010, 7, 19, 17, 0, 0);
string s = d.ToString("ddd dd MMM yyyy");
See Formatting a DateTime for display[^] for full details
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
How about you drop the "Local Time" and use DateTime.Parse/ParseExact/TryParse/TryParseExact ?
|
|
|
|
|
hi
how to open www.google.com and insert any text to the textbox search (only for show)
or another website that has textbox ?
in C# Winforms ?
thank's in advance
|
|
|
|
|
Hi,
not sure what you want.
1.
http://www.google.com/#q=any+text[^]
2.
Google Search has an API
3.
If there's only one editable control on a web page, it is likely to have the focus, so simply send some text (SendKeys) to the app.
|
|
|
|
|