Showing posts with label section. Show all posts
Showing posts with label section. Show all posts

Monday, March 26, 2012

System.Net.Mail

I have a class that contains(taken from faq section):

using System.Net.Mail;

public class MailHelper
{
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();

// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
//mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

ERROR HERE: "Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assisgned to--it is read only

mMailMessage.To = new MailAddressCollection().Add(to);

// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}

// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}

// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;

// Secify the format of the body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;

// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}

I call the method from the Form Submit button Like this:

private void GenEmails()
{

//Email Code Executed On Click Event
string from = EmailTextBox.Text;
string to = ContactIdentifier.EmailAddress;
string bcc = "null";
string cc = "null";
string subject = subjectTextBox.Text;
string body = DescriptionTextBox.Text;

MailHelper.SendMailMessage(from, to, bcc, cc, subject, body);
}

All I'm trying to do is pass the textbox.text values into the method as parameters. What am I doing wrong here?

why don't you put just:

mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

//ERROR HERE: "Property or indexer 'System.Net.Mail.MailMessage.To' cannot be assisgned to--it is read only

//mMailMessage.To = new MailAddressCollection().Add(to);

works for me...


// Set the recepient address of the mail message
//mMailMessage.To.Add(to);
//mMailMessage.To = new MailAddressCollection().Add(new MailAddress(to));

mMailMessage.To.Add(new MailAddress(to);

http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.to.aspx


Dim messageAsNew MailMessage("sender@.emailaddress.com", Session("destionationemailaddress"),"Order Alert","my e-mail body")

message.cc="sendanother@.emailaddresstothisaddress.com"

Dim emailClientAsNew SmtpClient("xxxxxxserver")

emailClient.Send(message)

when I add cc to the message object I get message.cc readonly error.

could you please help me with my problem.

thanks


use System.Web.Mail.MailMessage instead of System.Net.Mail.MailMessage.

Saturday, March 24, 2012

System.NullReferenceException

I hope this is the right forum section. I am a getting started. Please take a look at this I can't seem to get this data connection to work.

Sub Page_Load()
Teamlist.Datasource = getteams
teamlist.databind()
End Sub

Function GetTeams() As System.Data.IDataReader
Dim connectionString As String = _
ConfigurationSettings.AppSettings("connectionstring")
Dim dbConnection = ConfigurationSettings.AppSettings("connectionstring")

Dim queryString As String = "SELECT [Teams].[TeamID], [Teams].[TeamName], [Teams].[Notes] FROM [Teams]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

dbConnection.Open
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Now I get this:
Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error:

Line 21: dbCommand.Connection = dbConnection
Line 22:
Line 23: dbConnection.Open
Line 24: Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Line 25:

Here is the contents of my web.config

<?xml version="1.0" encoding="UTF-8" ?
<configuration
<!--

The <appSettings> section is used to configure application-specific configuration
settings. These can be fetched from within apps by calling the
"ConfigurationSettings.AppSettings(key)" method:

<appSettings>
<add key="connectionstring"
value="Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4;
Data Source=C:\begaspnet11\wroxunited\database\wroxunited.mdb"/>
</appSettings
--
<system.web
<!--

The <sessionState" section is used to configure session state for the application.
It supports four modes: "Off", "InProc", "StateServer", and "SqlServer". The
later two modes enable session state to be stored off the web server machine -
allowing failure redundancy and web farm session state scenarios.

<sessionState mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;trusted_connection=true"
cookieless="false"
timeout="20" /
--
<!--

The <customErrors> section enables configuration of what to do if/when an
unhandled error occurs during the execution of a request. Specifically, it
enables developers to configure html error pages to be displayed in place of
a error stack trace:

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
<customErrors
--
<!--

The <authentication> section enables configuration of the security authentication
mode used by ASP.NET to identify an incoming user. It supports a "mode"
attribute with four valid values: "Windows", "Forms", "Passport" and "None":

The <forms> section is a sub-section of the <authentication> section,
and supports configuring the authentication values used when Forms
authentication is enabled above:

<authentication mode="Windows"
<forms name=".ASPXAUTH"
loginUrl="login.aspx"
protection="Validation"
timeout="999999" /
</authentication
--
<!--

The <authorization> section enables developers/administrators to configure
whether a user or role has access to a particular page or resource. This is
accomplished by adding "<allow>" and "<deny>" sub-tags beneath the <authorization>
section - specifically detailing the users/roles allowed or denied access.

Note: The "?" character indicates "anonymous" users (ie: non authenticated users).
The "*" character indicates "all" users.

<authorization>
<allow users="joeuser" />
<allow roles="Admins" />
<deny users="*" />
</authorization
--
</system.web
</configuration>Hi,
Which line throw an error?
One problem I can see in your code is connection scope. DataReader can not be used if associated connection is closed. In your case connection (attached to command object) exists in scope of GetTeams scope and you are using DataReader out of this scope. It is wrong.
Line 23
dbconnection.open
Change this:


Dim dbConnection = ConfigurationSettings.AppSettings("connectionstring")

to:

Dim dbConnection as New SqlConnection()
dbConnection.ConnectionString = ConfigurationSettings.AppSettings("connectionstring")

dbConnection should be declared in the same scope where you are using associated datareader (see my previous reply)
1. Appreciate you working with me.

2. I'm using a Access mdb for this. How do that change this:

Dim dbConnection as New SqlConnection()

dbConnection.ConnectionString = ConfigurationSettings.AppSettings("connectionstring")

Thanks!
Rich
Sorry, I did not realize it is OleDb. Use this instead:


Dim dbConnection as New OleDbConnection()
dbConnection.ConnectionString = ConfigurationSettings.AppSettings("connectionstring")

Thanks but I got the same error.

The ConnectionString property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error:

Line 25: dbCommand.Connection = dbConnection
Line 26:
Line 27: dbConnection.Open
Line 28: Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Line 29:

Source File: C:\begasnet11\wroxunited\teams.aspx Line: 27

Stack Trace:

[InvalidOperationException: The ConnectionString property has not been initialized.]
System.Data.OleDb.OleDbConnection.Open() +203
ASP.teams_aspx.GetTeams() in C:\begasnet11\wroxunited\teams.aspx:27
ASP.teams_aspx.Page_Load() in C:\begasnet11\wroxunited\teams.aspx:9
System.Web.Util.ArglessEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +10
System.Web.UI.Control.OnLoad(EventArgs e) +55
System.Web.UI.Control.LoadRecursive() +27
System.Web.UI.Page.ProcessRequestMain() +731

------------------------

Dim dbConnection as New OleDbConnection(ConfigurationSettings.AppSettings("connectionstring"))
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand("SELECT [Teams].[TeamID], [Teams].[TeamName], [Teams].[Notes] FROM [Teams]", dbConnection)
Constructors are faster than instantiating and then initializing (admittedly you won't be noticing the speed gain, but still, it's good to have performance in mind at all times).
Still nothing. I've tried this on 2 diffrent computers. Grasping at straws.

Same error.

Thanks!
Rich
Was this statement (dbConnection.ConnectionString = ConfigurationSettings.AppSettings("connectionstring") ) executed?
If it was, check dbConnection.ConnectionString value in debugger. May be Config file entry was not read correctly.
I set debug to true and this is what I got in addition to the same error

[InvalidOperationException: The ConnectionString property has not been initialized.]
System.Data.OleDb.OleDbConnection.Open() +203
ASP.teams_aspx.GetTeams() in C:\begasnet11\wroxunited\teams.aspx:26
ASP.teams_aspx.Page_Load() in C:\begasnet11\wroxunited\teams.aspx:10
System.Web.Util.ArglessEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +10
System.Web.UI.Control.OnLoad(EventArgs e) +55
System.Web.UI.Control.LoadRecursive() +27
System.Web.UI.Page.ProcessRequestMain() +731
Man I feel so dumb. OK here it is. The <appsettings> in the webconfig file was commented out. I removed the comment tags and bang.

Thanks everyone!!
Does it matter if this is in a folder or a virtual directory? Does the web config file need a virtual directory to work in like a global asa?

Thursday, March 22, 2012

system.object to namevalue collection

I'm reading some stuff in a custom section of the
web.config file into a name value collection. What do I
need to do to make this work:

Dim nvcConfig As NameValueCollection = _
ConfigurationSettings.GetConfig
("myApp/mySection ")

With option strict on?See:

http://msdn.microsoft.com/library/d...ionsections.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

"Charles" <a@.b.c> wrote in message
news:0b3c01c36e2f$fca64820$a401280a@.phx.gbl...
> I'm reading some stuff in a custom section of the
> web.config file into a name value collection. What do I
> need to do to make this work:
> Dim nvcConfig As NameValueCollection = _
> ConfigurationSettings.GetConfig
> ("myApp/mySection ")
> With option strict on?

Tuesday, March 13, 2012

'System.ServiceModel/ServiceHostingEnvironment' configuration section cannot be c

On Jun 5, 11:22 pm, "Sachin" <Sachi...@dotnet.itags.org.Email.com> wrote:
> On both server its .NET 2.0
>
Did you installed the .NET 3.0 on the Server2?No.
This is what I get at the bottom of the error page.
Version Information: Microsoft .NET Framework Version: 2.0.50727.42; ASP.NET
Version:2.0.50727.210
"Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
news:1181079744.934238.48060@.q66g2000hsg.googlegroups.com...
> On Jun 5, 11:22 pm, "Sachin" <Sachi...@.Email.com> wrote:
> Did you installed the .NET 3.0 on the Server2?
>
re:
!> This is what I get at the bottom of the error page.
!> Version Information: Microsoft .NET Framework Version: 2.0.50727.42;
!> ASP.NET Version:2.0.50727.210
That looks like a W2K3 R2 install on Server2. Is it ?
Is it the same version which is installed on Server1 ?
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Sachin" <SachinGp@.Email.com> wrote in message news:uX0uSz7pHHA.3892@.TK2MSFTNGP05.phx.gbl..
.
> No.

> This is what I get at the bottom of the error page.
> Version Information: Microsoft .NET Framework Version: 2.0.50727.42; ASP.NET Versi
on:2.0.50727.210
> "Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
> news:1181079744.934238.48060@.q66g2000hsg.googlegroups.com...

'System.ServiceModel/ServiceHostingEnvironment' configuration section cannot be c

On Jun 5, 10:10 pm, "Sachin" <Sachi...@dotnet.itags.org.Email.com> wrote:
> Hi All,
> I deployed an ASP.NET Web Service on Server1 and it worked fine.
> However when I deployed the same Web Service on Server2 I am getting the
> following error,
> The 'System.ServiceModel/ServiceHostingEnvironment' configuration section
> cannot be created.
> The Machin.config file is missing information.
> Verify that this configuration section is properly registered and that you
> have correctly splled the section name.
> For Windoes Communication Foundation sections, run ServiceModelReg.exe -i
to
> fix this error.
> ...................
> I never saw this error. What could be wrong?
> Any pointer?
> Thanks in advance
> Sachin
Verify the version of ASP.NET.
aspnet_regiis -ls
aspnet_regiis -i
etc...On both server its .NET 2.0
"Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
news:1181074886.601979.301780@.n4g2000hsb.googlegroups.com...
> On Jun 5, 10:10 pm, "Sachin" <Sachi...@.Email.com> wrote:
> Verify the version of ASP.NET.
> aspnet_regiis -ls
> aspnet_regiis -i
> etc...
>

'System.ServiceModel/ServiceHostingEnvironment' configuration section cannot be created.

Hi All,

I deployed an ASP.NET Web Service on Server1 and it worked fine.
However when I deployed the same Web Service on Server2 I am getting the
following error,

The 'System.ServiceModel/ServiceHostingEnvironment' configuration section
cannot be created.
The Machin.config file is missing information.
Verify that this configuration section is properly registered and that you
have correctly splled the section name.
For Windoes Communication Foundation sections, run ServiceModelReg.exe -i to
fix this error.
....................

I never saw this error. What could be wrong?

Any pointer?

Thanks in advance
SachinOn Jun 5, 10:10 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

Hi All,
>
I deployed an ASP.NET Web Service on Server1 and it worked fine.
However when I deployed the same Web Service on Server2 I am getting the
following error,
>
The 'System.ServiceModel/ServiceHostingEnvironment' configuration section
cannot be created.
The Machin.config file is missing information.
Verify that this configuration section is properly registered and that you
have correctly splled the section name.
For Windoes Communication Foundation sections, run ServiceModelReg.exe -i to
fix this error.
...................
>
I never saw this error. What could be wrong?
>
Any pointer?
>
Thanks in advance
Sachin


Verify the version of ASP.NET.

aspnet_regiis -ls
aspnet_regiis -i
etc...
On both server its .NET 2.0

"Alexey Smirnov" <alexey.smirnov@.gmail.comwrote in message
news:1181074886.601979.301780@.n4g2000hsb.googlegro ups.com...

Quote:

Originally Posted by

On Jun 5, 10:10 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

>Hi All,
>>
>I deployed an ASP.NET Web Service on Server1 and it worked fine.
>However when I deployed the same Web Service on Server2 I am getting the
>following error,
>>
>The 'System.ServiceModel/ServiceHostingEnvironment' configuration section
>cannot be created.
>The Machin.config file is missing information.
>Verify that this configuration section is properly registered and that
>you
>have correctly splled the section name.
>For Windoes Communication Foundation sections, run ServiceModelReg.exe -i
>to
>fix this error.
>...................
>>
>I never saw this error. What could be wrong?
>>
>Any pointer?
>>
>Thanks in advance
>Sachin


>
Verify the version of ASP.NET.
>
aspnet_regiis -ls
aspnet_regiis -i
etc...
>


On Jun 5, 11:22 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

On both server its .NET 2.0
>


Did you installed the .NET 3.0 on the Server2?
No.

This is what I get at the bottom of the error page.

Version Information: Microsoft .NET Framework Version: 2.0.50727.42; ASP.NET
Version:2.0.50727.210

"Alexey Smirnov" <alexey.smirnov@.gmail.comwrote in message
news:1181079744.934238.48060@.q66g2000hsg.googlegro ups.com...

Quote:

Originally Posted by

On Jun 5, 11:22 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

>On both server its .NET 2.0
>>


>
Did you installed the .NET 3.0 on the Server2?
>


re:
!This is what I get at the bottom of the error page.
!Version Information: Microsoft .NET Framework Version: 2.0.50727.42;
!ASP.NET Version:2.0.50727.210

That looks like a W2K3 R2 install on Server2. Is it ?

Is it the same version which is installed on Server1 ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Sachin" <SachinGp@.Email.comwrote in message news:uX0uSz7pHHA.3892@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

No.


Quote:

Originally Posted by

This is what I get at the bottom of the error page.
>
Version Information: Microsoft .NET Framework Version: 2.0.50727.42; ASP.NET Version:2.0.50727.210


Quote:

Originally Posted by

"Alexey Smirnov" <alexey.smirnov@.gmail.comwrote in message
news:1181079744.934238.48060@.q66g2000hsg.googlegro ups.com...

Quote:

Originally Posted by

>On Jun 5, 11:22 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

>>On both server its .NET 2.0
>>>


>>
>Did you installed the .NET 3.0 on the Server2?


On Jun 5, 11:58 pm, "Sachin" <Sachi...@.Email.comwrote:

Quote:

Originally Posted by

No.
>


I don't know what your service does, but Windows Communication
Foundation required .NET 3.0