System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(yourNameTextBox.Text.Trim());
sb.Append("<br>");
sb.Append(yourEmailTextBox.Text.Trim());
Tuesday, 17 May 2011
How to Display Yes No message box & handle response in C# ASP.NET
Note:
Please remember javascript is type sensitive & whenever you copy something from internet check the code again for example change the double quotation mark and inverted coma sign.
Normal message box:
Response.Write("<script>alert('Hello')</script>");
Response.Write("<script> alert('" + myStringVariable + "') </script>");
Yes no message box:
Response.Write("<script language=JavaScript>confirm('Are you sure to continue?');</script>");
Yes no response:
. I write the code for .aspx page.
Please remember javascript is type sensitive & whenever you copy something from internet check the code again for example change the double quotation mark and inverted coma sign.
Normal message box:
Response.Write("<script>alert('Hello')</script>");
Response.Write("<script> alert('" + myStringVariable + "') </script>");
Yes no message box:
Response.Write("<script language=JavaScript>confirm('Are you sure to continue?');</script>");
Yes no response:
. I write the code for .aspx page.
======================================================================= <asp:Button ID="btnCommit" runat="server" Text="Commit"
OnClientClick="if(!confirm('Are you quite sure?')) return false;"
onclick="btnCommit_Click" />
</asp:Content>
=======================================================================
OnClientClick="if(!confirm('Are you quite sure?')) return false;"
onclick="btnCommit_Click" />
</asp:Content>
=======================================================================
Copy and paste following code in your code behind(aspx.cs) page.
=======================================================================
protected void Page_Load(object sender, EventArgs e)
{}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Yes button clicked.')</script>");
}
=======================================================================
2nd method:
use javascript in .aspx
<script language="javascript" type="text/javascript">
function confirm()
{
if (confirm ==true)
do something;
else
return false;
}
</script>
then write in .aspx.cs
ButtonSave.Attributes.Add("onclick", "return confirm('Are you sure you proceed?');");
2nd method:
use javascript in .aspx
<script language="javascript" type="text/javascript">
function confirm()
{
if (confirm ==true)
do something;
else
return false;
}
</script>
then write in .aspx.cs
ButtonSave.Attributes.Add("onclick", "return confirm('Are you sure you proceed?');");
Sunday, 15 May 2011
Send email using C# ASP.NET
In Web.config
<!--
<system.net>
<mailSettings>
<smtp from="info@bdculture.com">
<network
host="mail.bdculture.com"
port="25"
userName="info@bdculture.com"
password="info123"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
-->
===========================
***************************
SmtpClient smtp = new SmtpClient();
smtp.Host = SimpleTopupConfiguration.MailServer;
smtp.Credentials = new System.Net.NetworkCredential("info@bdculture.com", "info123");
smtp.Port = 25;
MailMessage mail = new MailMessage();
string from = SimpleTopupConfiguration.FromMail;
mail.From = new MailAddress(from);
string sendTo = SimpleTopupConfiguration.SendTo;
mail.To.Add(sendTo);
string subject = subjectTextBox.Text.ToString();
mail.Subject = subject;
string body =
"Name: " + nameTextBox.Text.ToString()
+ "\n" + "Organization: " + organizationTextBox.Text.ToString()
+ "\n" + "Telephone: " + telephoneTextBox.Text.ToString()
+ "\n" + "Email from: " + emailTextBox.Text.ToString()
+ "\n" + "Message: "
+ "\n" + commentsTextBox.Text.ToString();
mail.Body = body;
smtp.Send(mail);
*************************
Web.config:
<appSettings>
<add key="MailServer" value="mail.pcl.ac"/>
<add key="Port" value="25"/>
<add key="FromMail" value="masud.ahmed@pcl.ac"/>
<add key="Keyword" value="!1pcl000"/>
<add key="SendToMail" value="masuds_mail@yahoo.com" />
</appSettings>
Mail function .aspx.cs file:
protected void sendEmailButton_Click(object sender, EventArgs e)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = MasudAhmedConfiguration.MailServer;
smtp.Port = Int32.Parse(MasudAhmedConfiguration.Port);
smtp.Credentials = new System.Net.NetworkCredential(MasudAhmedConfiguration.FromMail, MasudAhmedConfiguration.Keyword);
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(MasudAhmedConfiguration.SendToMail);
mailMsg.From = new MailAddress(MasudAhmedConfiguration.FromMail);
mailMsg.Subject = "Test Email";
string body = bodyTextBox.Text.ToString();
mailMsg.Body = body;
//string senderEmail = yourEmailTextBox.Text.ToString();
//mailMsg.From = new MailAddress(senderEmail);
try
{
smtp.Send(mailMsg);
}
catch(Exception ex)
{
throw ex;
}
}
===================
Email address validation check:
<!--
<system.net>
<mailSettings>
<smtp from="info@bdculture.com">
<network
host="mail.bdculture.com"
port="25"
userName="info@bdculture.com"
password="info123"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
-->
===========================
<system.net> <mailSettings> <smtp deliveryMethod="Network" from="myid@yahoo.com"> <network host="smtp.mail.yahoo.com" port="465" userName="myid@yahoo.com" password="mypassword" defaultCredentials="true" /> </smtp> </mailSettings> </system.net>
SmtpClient smtp = new SmtpClient();
smtp.Host = SimpleTopupConfiguration.MailServer;
smtp.Credentials = new System.Net.NetworkCredential("info@bdculture.com", "info123");
smtp.Port = 25;
MailMessage mail = new MailMessage();
string from = SimpleTopupConfiguration.FromMail;
mail.From = new MailAddress(from);
string sendTo = SimpleTopupConfiguration.SendTo;
mail.To.Add(sendTo);
string subject = subjectTextBox.Text.ToString();
mail.Subject = subject;
string body =
"Name: " + nameTextBox.Text.ToString()
+ "\n" + "Organization: " + organizationTextBox.Text.ToString()
+ "\n" + "Telephone: " + telephoneTextBox.Text.ToString()
+ "\n" + "Email from: " + emailTextBox.Text.ToString()
+ "\n" + "Message: "
+ "\n" + commentsTextBox.Text.ToString();
mail.Body = body;
smtp.Send(mail);
*************************
Web.config:
<appSettings>
<add key="MailServer" value="mail.pcl.ac"/>
<add key="Port" value="25"/>
<add key="FromMail" value="masud.ahmed@pcl.ac"/>
<add key="Keyword" value="!1pcl000"/>
<add key="SendToMail" value="masuds_mail@yahoo.com" />
</appSettings>
Mail function .aspx.cs file:
protected void sendEmailButton_Click(object sender, EventArgs e)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = MasudAhmedConfiguration.MailServer;
smtp.Port = Int32.Parse(MasudAhmedConfiguration.Port);
smtp.Credentials = new System.Net.NetworkCredential(MasudAhmedConfiguration.FromMail, MasudAhmedConfiguration.Keyword);
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(MasudAhmedConfiguration.SendToMail);
mailMsg.From = new MailAddress(MasudAhmedConfiguration.FromMail);
mailMsg.Subject = "Test Email";
string body = bodyTextBox.Text.ToString();
mailMsg.Body = body;
//string senderEmail = yourEmailTextBox.Text.ToString();
//mailMsg.From = new MailAddress(senderEmail);
try
{
smtp.Send(mailMsg);
}
catch(Exception ex)
{
throw ex;
}
}
===================
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx@gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
Email address validation check:
Step 1: Simple validation
Simple validation basically checks that there's one "@" and one ".".
throw new ArgumentNullException( "inputEmail" );
}
public static bool isEmail(string inputEmail)
{
if( inputEmail == null || inputEmail.Lengt == 0 )
{throw new ArgumentNullException( "inputEmail" );
}
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
Step 2: DNS validation
The first real step is to make sure that the domain actually exists.Step 3: SMTP validation
This method uses the SMTP protocol to contact the domain mailserver and actually ask if the user is valid!
Button is not firing in User Control - ASP.NET
Button does not work in User Control. (May be no control does work inside a User Control.)
Solution:
Use aspx web form to make your page and then use iFrame inside a User Control to call that web form.
Solution:
Use aspx web form to make your page and then use iFrame inside a User Control to call that web form.
Friday, 6 May 2011
GridView: Get the website link from Database
<asp:GridView ID="grid" runat="server" DataKeyNames="WebsiteID" Width="100%"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Website Descriptions"
SortExpression="Descriptions">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Descriptions") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Descriptions") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFormatString=""
DataTextField="Link" HeaderText="Website Link" DataNavigateUrlFields="Link" Target="_blank" />
</Columns>
</asp:GridView>
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Website Descriptions"
SortExpression="Descriptions">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Descriptions") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Descriptions") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFormatString=""
DataTextField="Link" HeaderText="Website Link" DataNavigateUrlFields="Link" Target="_blank" />
</Columns>
</asp:GridView>
Wednesday, 4 May 2011
SQL: Cannot open database .... requested by the login. The login failed. Login failed for user ‘NT AUTHORITY\NETWORK SERVICE’.
Cause:
This error occurs when you have configured your application with IIS, and IIS goes to SQL Server and tries to login with credentials that do not have proper permissions. This error can also occur when replication or mirroring is set up.
This error occurs when you have configured your application with IIS, and IIS goes to SQL Server and tries to login with credentials that do not have proper permissions. This error can also occur when replication or mirroring is set up.
If you search online, there are many different solutions provided to solve this error, and many of these solutions work fine. However, I will be going over a solution that works always and is very simple.
Fix/Workaround/Solution:
- Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
- In newly opened screen of Login Properties,
- go to the “User Mapping” tab.
- Then, on the “User Mapping” tab,
- select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.
- If it is Database-->Properties-->Permission-->Server permission-->Security--> Server authentication is set to Windows Authentication then add
- "BUILTIN\Users" user to the user mapping and db_owner access role to the database fixed the problem
It has worked for me.
SQL: Login failed for user - computername\aspnet - ASP.NET
- Open SQL Server Management Studio
- Create a Database if it is not created already
- In the root folder of the SQL Server Management Studio there is a -->Security folder-->Expand it
- In order to create login-->Select Login --> Right click
- Put a login Name
- Select SQL Server authentication if you want
- Uncheck - enforce password policy
- type password
- Select the default database (optional)
- User mapping--> (if you select this option the user name with same login name will be created automatically otherwise you have created manually)
- You can select the database you want to associate &
- Select the rights
- dbowner
- datareader
- datawriter
- Creating create user name manually
- Select the database and expand -->Security-->Users
- Put a user name (it is better to give a same user name as login name)
- Select the login name you just have created
- In the Database role membership select the roles:
- dbowner
- datareader
- datawriter
Subscribe to:
Posts (Atom)