Monday, March 12, 2012

Getting out of form

I am trying to add records to SQL database, thru <form> </form> on click of button, The system is working fine, Databse table is getting updated correctly.

But screen displays the same form again while i want it to open another asp.net page i.e. buy.aspx and also generate an e-mail automatically.

I m stuck. Please help.

pcg

Relevent code of mine is as below.

<script runat="server">

void addtosalelist(Object sender, EventArgs e)
{
...
...
dbConnection.Open();
...
...
dbConnection.Close();
}

<form action="buy.aspx" method="post" runat="server">
...
...
...
<asp:Button id="button1" onclick="addtosalelist" runat="server" Text="Submit"></asp:Button>

</form>

After adding to the database do:

Response.Redirect("buy.aspx");

|||

If your submit button event handler is addtosalelist then:

using System.Net.Mail;

public void addtosalelist(object sender, EventArgs e)
{
AddToDatabase();
SendEmail();
Response.Redirect("Buy.aspx");
}

private void AddToDatabase()
{
//Add to the database
}

private void SendEmail()
{
//Send your email

string subject ="This is the subject";
string body ="This is the body";
MailMessage mm =newMailMessage("From@.test.com","to@.test.com", subject, body);
mm.IsBodyHtml =true;//Or false for plain text

SmtpClient client =newSmtpClient();
client.Host ="192.168.0.1";//IP address of mail server
client.Port = 25;//Port number
client.Send(mm);
}

|||

pcg:

<script runat="server">

void addtosalelist(Object sender, EventArgs e)
{
...
...
dbConnection.Open();
...
...
dbConnection.Close();

Response.Redirect("buy.aspx", false);

}

<form action="buy.aspx" method="post" runat="server">
...
...
...
<asp:Button id="button1" onclick="addtosalelist" runat="server" Text="Submit"></asp:Button>

</form>

|||

Dear DavidKiff,

While the Response. Redirect(); works fine, SendEmail() does not compile.

There are two problems.

1. The body of the message is to use four variables, two i.e. name and email address are to be picked from form text boxes, and one from database and one passwordto be generated. It does not compile with error New Line in constant.

2. It does not accept the MailMessage

pl see if u can help.

pcg

|||

Dear Mr. OmerKamal,

Thanks, itworks.

pcg

No comments:

Post a Comment