Sunday 14 October 2012

How to Insert data in data table using LINQ

How to Insert data in data table using LINQ

Answer is simple. 

First of all create a LINQ to SQLClass. For this Right Click on the project Name -> Add-> New Item->Then select Data-> then select LINQ to SQLClass and change then name of the class and press ADD Button. Here the class name is RegClass. Remember when you are changing the name the class name "DataContext" will be added with the name. For example here the Full name is RegClassDataContext. Now drag and drop the data table from server explorer to the RegClassDataContext and write the following code.

Here code is writen in a method called SaveData() which will be called where needed.(Here mstRegistration is data table)

 private void btnCalcel_Click(object sender, EventArgs e)
        {
            RegClassDataContext reg = new RegClassDataContext();
            mstRegistration obj = new mstRegistration();
            obj.Adress = txtAddress.Text;
            obj.Age = Convert.ToInt32(txtAge.Text);
            obj.ContactNo=Convert.ToDecimal(txtContactNo.Text);
            obj.AlternativeContactNo = Convert.ToDecimal(txtAlternateContactNo.Text);
            obj.Name = txtName.Text;
            obj.OPD = txtOPDReffered.Text;
            obj.Problem = txtProblem.Text;
            obj.RegistrationNo =Convert.ToDecimal( txtRegistration.Text);
            obj.RegistrationFee = Convert.ToInt32(txtRegistration.Text);
            obj.RegistrationYear = System.DateTime.Now.Year;
            reg.mstRegistrations.InsertOnSubmit(obj);
            reg.SubmitChanges();
        }

How to add data in datatable using LINQ

How to add data in datatable using LINQ in ASP.Net

Answer is very easy. First of all create a LINQ to SQLClass. For this Right Click on the project Name -> Add-> New Item->Then select Data-> then select LINQ to SQLClass and change then name of the class and press ADD Button. Here the class name is RegClass. Remember when you are changing the name the class name "DataContext" will be added with the name. For example here the Full name is RegClassDataContext. Now drag and drop the data table from server explorer to the RegClassDataContext and write the following code.

Here code is writen in a method called ShowData() which will be called where needed.(Here DatagridView1 is a grid view where data is showing and mstRegistrations is data table)

   public void ShowData()
        {
            RegClassDataContext reg = new RegClassDataContext();
            var query = from i in reg.mstRegistrations select i;
            dataGridView1.DataSource = query;
          
        }

For selecting data from multiple datatable (mstRegistrations and fee are data table):

public void ShowData()
        {
            RegClassDataContext reg = new RegClassDataContext();
            var query = from i in reg.mstRegistrations
                        from j in reg.fee select new { a, b };
            dataGridView1.DataSource = query;
          
        }

Thursday 27 September 2012

Sending Multiple mail in a queue

Sending Multiple mail in a queue in asp.net:

 string strTo = txtTo.Text;
        for (int i = 0; i < strTo.Split(',').Length;i++ )
        {
             string strMailId = strTo.Split(',')[i];
             try
            {
                MailMessage msg = new MailMessage();
                msg.From = txtFrom;
                msg.To=txtTo;
                msg.Subject = txtSubject.Text;
                msg.IsBodyHtml = true;
                msg.Body = txtMail.Text;
                msg.Priority = MailPriority.High;

                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 25);
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("xxx@gmail.com", "xxx"); //Email and Password

                client.EnableSsl = true;

                client.Send(
msg );
           
                ScriptManager.RegisterStartupScript(btnSend, typeof(Page), "Confirm", "alert('Your enquiry sent successfully!');formReset();", true);
            }
            catch (Exception Ex)
            {
                ScriptManager.RegisterStartupScript(btnSend, typeof(Page), "Error:", "alert('Oops! Some error occoured.')", true);
            }
        }


Monday 3 September 2012

How can I send Email in ASP.Net using C#

How can I send Email in ASP.Net using C#

Answer:
You can use the following code for sending Email:
 MailMessage msg = new MailMessage();
            msg.From = txtFrom.Text);
            msg.To=txtTo,text;
            msg.Subject =txtSubject
            msg.IsBodyHtml = true;
            msg.Body =txtBody.text;
            msg.Priority = MailPriority.High;

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 25);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("xxxxx@gmail.com", "xxxx"); //  Mail Id and it's Password

            client.EnableSsl = true;

            client.Send(msg);

If any suggestion OR any question regarding ASP.Net then drop a mail to mnalammwb@gmail.com


How can I convert a Number to Roman No in asp.Net using c#

How can I convert a Number to Roman No in asp.Net using c#?

Answer:


    static private string Roman(int i)
        {
            return Thounds(i);
        }
        static private string Thounds(int i)
        {
            string strNo = "";
            for (int x = 0; x < i / 1000; x++)
            {
                strNo =strNo+ "M";
            }
            strNo = strNo + UptoThousand(i % 1000);
            return strNo;
        }
        static private string UptoThousand(int i)
        {
            string strNo="";
             for (int x = 0; x < i/100; x++)
                {
                    strNo = strNo + "C";
                }
             if (strNo.Length == 4)
             {
                 strNo = "CD";
             }
             else if (strNo.Length == 5)
             {
                 strNo = "D";
             }
             else if (strNo.Length == 9)
             {
                 string temp  = "CM";
                 strNo = temp;
             }
             else if (strNo.Length >5 && strNo.Length <9)
             {
                 string temp = strNo.Substring(0, 5);
                 temp = "D";
                 strNo = temp + strNo.Substring(5);
             }
             strNo = strNo + UptoHundreds(i%100);
             return strNo;

        }
        static private string UptoHundreds(int i)
        {
            string strNo = "";
            int k = i % 10;
            for (int x = 0; x < i / 10; x++)
                strNo = strNo + "X";
            if (strNo.Length == 4)
            {
                strNo = "IL";
            }
            else if (strNo.Length == 5)
            {
                strNo = "L";
            }
            else  if (strNo.Length == 9)
            {
                string temp = "XC";
                strNo=temp ;
            }
            else if (strNo.Length > 5 && strNo.Length < 9)
            {
                string temp = strNo.Substring(0, 5);
                temp = "L";
                strNo = temp + strNo.Substring(5);
            }
            strNo = strNo + UpToTen(i % 10);
         
            return strNo;
        }      
        static private string UpToTen(int i)
        {

            string strNo = "";
            for (int x = 1; x <= i; x++)
            {
                strNo = strNo + "I";
            }

            if (strNo.Length == 4)
            {
                strNo = "IV";
            }
            else if (strNo.Length == 5)
            {
                strNo = "V";
            }
            else if (strNo.Length == 9)
            {
                string temp = "IX";
                strNo = temp;
            }
            else if (strNo.Length > 5 && strNo.Length < 9)
            {
                string temp = strNo.Substring(0, 5);
                temp = "V";
                strNo = temp + strNo.Substring(5);
            }
            return strNo;
        }
 
If any one to suggest me OR ask me any thing more then mail me to mnalammwb@gmail.com