Showing posts with label unknown. Show all posts
Showing posts with label unknown. Show all posts

Saturday, March 24, 2012

Strange error with SqlDataReader

Hi,
I am using a datareader to fill a datagrid in a usercontrol. I use this method a lot in my application but for an unknown reason, I get this error message in only one web form: "Invalid attempt to FieldCount when reader is closed". What is strange is that the reader is actually open when the error is occuring. The only thing I see that could cause an error is that the user control is created from another usercontrol just after closing another SqlDataReader:

Calling user control


SqlDataReader rdr = cmd.ExecuteReader();
childUC = (DynamicUC)LoadControl( rdr["ASCXDynPage"].ToString() + ".ascx" );
childUC .Language = rdr["NoLanguage"].ToString();
rdr.Close();
childUC.DataBind();

Called user control (DataBind method)

... Creating SQLCommand here ...
SqlDataReader rdr = cmd.ExecuteReader();
gridNews.DataSource = rdr;
gridNews.DataBind();
gridNews.Visible = true;
rdr.Close();

After some tests, the error seam to happen when I set the DataSource of the DataGrid with the DataReader. Also, the error is set at the line where I call the DataBind method of the UserControl childUC. Anyone ever saw that kind of behavior?Maybe your query hasn´t data.
I suggest you use a dataset as datasource of the datagrid.
The query has data.
I tried the dataset, it works. But I really want to know what is wrong with that piece of code... I really don't understand what is going on...

Thursday, March 22, 2012

Strange issue with system.web.mail

Hello,

I have a strange issue when sending e-mails using system.web.mail. For an unknown reason every 990 character is a space in my message body. Not a big issue? Well, bigger then you can think. If the 990 character is in the middle of a HTML-tag, the message body is constructed wrong.

I tested this with the script you can find below. Try it yourself to see the result. I use Microsoft .NET Framework Version:1.1.4322.2300; ASP.NET Version:1.1.4322.2300. My provider doesn't have another version available for me.

<%@dotnet.itags.org. Import Namespace = "System.Text" %>
<%@dotnet.itags.org. Import Namespace = "System.Web.Mail" %>
<script runat="server">
Sub SubmitEmail(sender As Object, e As EventArgs)
Dim sMsg as String
sMsg ="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"
sMsg+="123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-"

doEmail("name.to@dotnet.itags.org.yourhost.com", "name.from@dotnet.itags.org.yourhost.com", sMsg, "Test e-mail")
End Sub

Function doEmail(sTo as String, sFrom as String, sMsg as String, sSubject as String) 'Email component
Dim objEmail as New MailMessage
objEmail.To=sTo 'TO
objEmail.FROM=sFrom 'From

objEmail.SUBJECT=sSubject 'Subject
objEmail.BODY=sMsg 'Link Message body
objEmail.BodyFormat = MailFormat.HTML 'MailFormat.Text = RichText & MailFormat.HTML = HTML
SmtpMail.SmtpServer ="localhost" 'SMTP server
SmtpMail.Send(objEmail) 'Send message
End Function
</script>
<form runat="server">
<asp:Button id="Submit" text="Submit e-mail" OnClick="SubmitEmail" runat="server" />
</form>

Is this a known issue? What is the solution? I prefer not buying a e-mail component?

Thanks for your suggestions.

I don't know the answer - but have you tried stringbuilder. I have read about problems using string ( mostly performance related ) and that stringbuilder is the best choice ( for most string building situations)

This issue is solved. I found the solution here:

http://www.systemwebmail.com/faq/4.4.5.aspx

Usually you have a line in the body that is too long. The email rfc's state that a line should not contain more than 1000 characters. Try inserting a CrLf (vbCrLf for VB.NET or "\r\n" for C#) in your message body to break up long lines.