connecting to a mySql database from asp.net application

asp.net to mySql connection

There are times when you want to connect to a mySql database from your asp.net code. By default the drivers for mySql are not packaged with asp.net so you need to include an external dll into your project.

MySql.data.dll

this dll contains all the necessary code required to provide the abstraction that .net developers are used to with while working on ADO.NET. You add the dll into your project and next moment you are creating instances of MySqlConnection, MySqlDataReader and hence forth things are same as you would do with your mssql database connection code.
The dll is available to download at the end of this article.

How to use

  1. download MySql.data.dll
  2. copy it to the bin folder of your application (use windows explorer to browse to the project location (bin folder as it’s generally not visible in Visual Studio)
  3. Add a reference of the said dll into your project
  4. Add using MySql.Data.MySqlClient; inside your code
  5. Make objects of MySqlConnection, MySqlDataReader, MySqlCommand and use them as you would use SqlConnection, SqlDataReader, SqlCommand respectively

contain original width even after padding

What is it

its a common problem that many new-by web-designers come across with. You have a div of predefined width which you don’t wanna change, and now you want to give the <div> some padding so that the content looks better visually. The moment you give and padding to the <div> its width increases and breaks your design !!.
Lets say you have a container <div> of width 600px and you want to float 3 div’s of equal width in there. So you set each inner <div> a width of 200px. so far so good, its working. But you see all the 3 inner <div> have no space between them and you go to set a margin to separate them a bit. You give the inner div’s a margin of 10px each and to compensate you reduce their width to 180px. Phew !! it was hard but it worked..

Again now you want to give some padding to the div’s. You give them a padding of 10px and your design breaks again !!!

Reason

padding increases the width of the container. Normally in the beginning we think (and that’s how it should be i think) that padding wont affect the outer width, instead it will leave space inside the border and the content will get less space to fit in. But actually what happens is that the space inside remains same and total width increase to accommodate for padding space. So a <div> with width 100px on giving a padding of 10px actually becomes 100+(10*2) = 120px wide.

The solution

Its rather easier than you think it would be. Add another <div> inside your content <div> and give the inner <div> a padding of 10px, then wrap all your content inside the inner <div>
so originally if i had my markup as

<div class="container">lorem ipsum lorem ipsum lorem ipsum lorem ipsum</div>

the css →→ .container{width:100px}
I’ll change it to something like

<div class="container">
<div class="inPadd">lorem ipsum lorem ipsum lorem ipsum lorem ipsum</div>
</div>

extra css →→ .inPadd{padding:10px}

That does the trick for you. So wherever you want some padding you just wrap around the content in a <div> with class ‘inPadd’ and it wont affect the width of your outer container.

Hope it helps…

sending sms from your asp.net application

send free SMS from your asp.net application/website using third party services. A sample project is available for download at the end of this article.

What is it

The provided dll contains a class Freesmsapi which defines multiple methods to send sms’es from your asp.net website. The methods uses the credentials from your account on freesmsapi.com website. In order to use this you need to first create an account on the said website [Yes, its free !! 🙂]. You get a key which you pass as a parameter to the constructor of freesmsapi class of wdw_utilities.dll. So essentially your application sends a request to the said website which validates the request against the authentication key provided and then sends the sms.

There are few restrictions though :

  • You can send only 100 sms per day (but that should be sufficient for a small app)
  • Additionally according to R.T.I norms you can’t send sms between 9 in night till 9 in morning
  • Your sms’es are not delivered to numbers registered in DND

If all these restriction doesn’t stop you form using it then go ahead download the source code and use it the way you want.

How to use

  1. create an account on freesmsapi.com and get your authentication key
  2. create a new web application project in visual studio
  3. Add wdw_utilities.dll to the bin folder of your newly created project
  4. Add a reference to the said dll into your project
  5. add using wdw_utilities; to your code file
  6. create object(s) of the freesmsapi class passing your authentication key as parameter and call the functions passing appropriate values

How to use

class freesmsapi defines 2 overloads of method sendSMS. You pass the appropriate parameters (receivers mobile no and your text message) to the method and it sends the SMS.

  1. sendSMS(string mobile_numbers, string message) : ‘mobile_numbers’ is comma separated list of mobile numbers to whom who want to send the SMS. Valid values are ‘9*********’ (just one recepient) or ‘9*********, 9*********, …’ (just many recepient)
    ‘message’ is your content of the sms.
  2. sendSMS(string[] mobile_numbers, string message) : ‘mobile_numbers’ is a string array containing list of mobile numbers to whom who want to send the SMS. e.g: [‘9*********’, ‘9*********’, …]
    ‘message’ is your content of the sms.

method returns a string value which contains the response from freesmsapi website.
Thats it in here…..
hope you find it usefull.. cheers..

sending email attachement using smtp in asp.net

send emails with or without attachment from your asp.net application/website. A sample project is available for download at the end of this article.

What is it

The provided project shows how to send emails (with or without attachment) . The project uses wdw_utilities.dll which has methods defined to send emails.. Please download the project and open it in visual studio to refer to as we go through the code. The project requires you to provide smtp credentials. So create an email account on your hosting server (if you have one) or you can use gmail’s SMTP server.

For those who are wondering what the heck is SMTP :

in simple terms you use one email accounts credentials to send emails to others email accounts. If you have a hosting account then go create a new email account something like ‘no-reply@yourdomain.com’. Your hosting provider will inform you about the smtpserver, smtp port etc.. your emailId becomes the smtpusername and your email accounts password becomes the smtppassword.
Alternatively you can use gmail’s smtp server. Below are the details :

smtp server : smtp.gmail.com
smtp port : 587
smtp username : yourmail@gmail.com
smtp password : your gmail accounts password

**Please note : if you are using gmail’s smtp details then you must set useSSL to true.

Lets see some code

Open email.aspx.cs from the file list in solution explorer.
The line using wdwUtilities; adds a reference to the wdwUtilities.dll. You will find following code inside btn_sendemail_click() function (the click event of send email button)

string _smtphost = txt_smtphost.Text.Trim();
int _smtpport = Convert.ToInt32(txt_smtpport.Text.Trim());
string _smtpuser = txt_smtpuname.Text.Trim();
string _smtppassword = txt_smtppassword.Text.Trim();
Email obj = new Email(_smtphost, _smtpport, _smtpuser, _smtppassword, useSSL);

bool success = obj.sendEmail(txt_from_email.Text.Trim(), txt_to_email.Text.Trim(), txt_subject.Text.Trim(), txt_message.Text.Trim(), ref errs);

if (success) { master_hiddenFld.Value = "Email sent successfuly"; }
else { master_hiddenFld.Value = "Error sending email : \n" + errs; }

the lines string _smtphost = txt_smtphost.Text.Trim();.... sets things up for creating the object of Email class (defined in wdwUtilities namespace).

Email obj = new Email(_smtphost, _smtpport, _smtpuser, _smtppassword, useSSL);

parameters are self-explanatory i hope. The last parameter useSSL tells if you wanna use SSL for communication. If you are not sure whether it should be true or false then leave it as false. But make sure if you are using gmail’s smtp details then you set it to true.
Then you call the sendEmail() method of Email class passing appropriate values.

bool success = obj.sendEmail(txt_from_email.Text.Trim(), txt_to_email.Text.Trim(), txt_subject.Text.Trim(), txt_message.Text.Trim(), ref errs);

this method sends the email.
the return type is bool, the returned value of true indicates the email was sent successfuly while false means there was some error 🙁
parameters : from_email and to_email are self-explanatory.
subject is the emails subject and message is the entire body of your email. You can write HTML inside the message string.
The last parameter (passed by reference) brings back the erros(if any) that occured while trying to send email.
There are multiple overloads of the methods in Email class, try them as well.
That’s it as of now.. do post your comments…
hope you find it usefull.. cheers..