How to get date for every sunday in a month using asp.net with c#

In this programming tutorial you will learn how to get the date for every sunday in a month using asp.net with c#. There are multiple methods to achieve our goal. Let`s have a look over them.

How to get date for every sunday in a month in asp.net using c#

Method 1
using System.Globalization;//Donot forget to declare this namespace
 protected void Page_Load(object sender, EventArgs e)
    {
 DateTime currentDateTime=System.DateTime.Now;
        int year=currentDateTime.Year;
        int months=currentDateTime.Month;
        GetDatesOfSundays(year, months, DayOfWeek.Sunday);
    }

    protected void GetDatesOfSundays(int year, int month, DayOfWeek dayName)
    {
        CultureInfo ci = new CultureInfo("en-US");
        for (int i = 1; i <= ci.Calendar.GetDaysInMonth(year, month); i++)
        {
            if (new DateTime(year, month, i).DayOfWeek == dayName)
                Response.Write(i.ToString() + ",");
        }
    }

Output
1,8,15,22,29

Method 2
protected void Page_Load(object sender, EventArgs e)
    {
     Response.Write(GetDatesOfSundays(System.DateTime.Now));

    }
private string GetDatesOfSundays(DateTime DatMonth)
    {
        string sReturn = "";
        int iDayOffset = DatMonth.Day - 1;
        DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
        DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
        while (DatMonth < DatMonth2)
        {
            if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
            {
                if (sReturn.Length > 0) sReturn += ",";
                sReturn += DatMonth.ToShortDateString();
            }
            DatMonth = DatMonth.AddDays(1.0);
        }
        return sReturn;
    } 
Output
5/1/2011,5/8/2011,5/15/2011,5/22/2011,5/29/2011

So these are the methods to find the sundays in a month using asp.net with c#.

I hope you will find this tutorial very informative.

I love your feedback.
Read more...

How to get the all user created tables saved in ms sql server database

In this programming tutorial you will learn how to get the all user created tables saved in ms sql server database. It is quite easy and simple. Only you have to do is to write and execute the following query.

How to get all user created tables saved in ms sql server database

SELECT * FROM SYS.OBJECTS WHERE TYPE ='u'
The above mentioned query will return you the all user created tables saved in ms sql server database. User created tables means the tables created by users, not the built-in system tables. Sys.Objects is a built-in table in ms sql server that has information about each and every element of the database.

I love your feedback.
Read more...

Query to get the length of string in ms sql server

In this programming tutorial you will learn the query that will get the length of string in ms sql server. It is quite easy in ms sql server. Let's have a look over it.

Query to get the length of string in ms sql server

select len('Arman Malik') as length;

Output
11

The above query will give you 12 as output because there are total 11 characters including space found in string.

That's it. Using built-in len() function of ms sql server you can do this very easily. In case if there is any apostrophe in string then you have to use another apostrophe before it like illustrated in example given below
select len('Arman Malik''s passion is programming') as length;
Output:-
37

I hope you will found this tutorial very handy and informative.

I love your feedback.
Read more...

Redirection after some time delay in php


In this programming tutorial we will learn how to perform redirection after some time delay in php. I have already written tutorials for same topic in javascript and asp.net. It is quite easy in php. I will demonstrate this with two methods. So let's have a look over how to do so.

Redirection after some time delay in php


Use Header Function
header("refresh:5;url=somepage.php");
Note:- If your php page also contains html code then use header function on very top of the page just before the <head> tag else you will get the headers already sent by... error.

Use Meta tag in echo or print statement
print "<meta http-equiv='refresh' content='5;url=somepage.php'>";

So that's it. I hope you will find this tutorial very handy.

I love your feedback

Read more...

Insertion of records in database using 3-tier architecture in asp.net with c#

In this tutorial we will discuss about how to insert records in database using 3-tier architecture in asp.net with c#. For beginners I strongly recommend to read this tutorial 3-Tier Architecture in asp.net using c# first to get basics of 3-tier architecture in asp.net. So let’s start.
3-Tier Architecture in asp.net

Insertion of records in database using 3-tier architecture in asp.net with c#

insert-records.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert-records.aspx.cs" Inherits="insert-records" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Insertion of records in database using 3-tier architecture in asp.net with c#</title>
</head>
<body>
<form id="form1" runat="server">
  <table width="600px" cellspacing="2" cellpadding="2" border="1" style="font-family:verdana;font-size:12px;">
    <tbody>
      <tr>
        <td colspan="2" align="left"><strong> User Feedback Form</strong></td>
      </tr>
      <tr>
        <td align="left" width="20%"> First Name </td>
        <td><asp:TextBox ID="txtFirstName" runat="server" Font-Size="Small" Width="250px"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td align="left"> Last Name </td>
        <td align="left"><asp:TextBox ID="txtLastName" runat="server" Font-Size="Small" Width="250px"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td> Feedback </td>
        <td><asp:TextBox ID="txtFeedback" runat="server" TextMode="MultiLine" Rows="12" Columns="50"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align="left"><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
        </td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</html>
As you have seen in code and image that we have two textboxes, one textarea and one button in our .aspx page. User will fill the form and click the button to submit his/her feedback. At button’s click event in code behind I have written all the logic to insert records in database using 3-tier architecture in asp.net with c#. Remember I have not performed any client side or server side validation on my form fields because I want to make code simple and clear for you and just want to focus on topic, so let’s have a look over its code behind file

insert-records.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer;//Donot forget to import this namespace


public partial class insert-records : System.Web.UI.Page
{
BusFeedback _objFeedback = new BusFeedback ();
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
    {
_objFeedback.InsertFeedback(txtFirstName.Text, txtLastName.Text, txtFeedback.Text);
 //If records successfully entered then redirect to feedback page
//Response.Redirect("view-feedback.aspx");
    }

}



I have declared the object of my bus BusFeedback that I am going to create after this explanation. The object that I declared is _objFeedback then on click event of asp:button I made call to the function written in BusFeedback, that will insert records in database and that’s it. I could write insertion function in BusComments that I created in my previous tutorial but for better understanding I am creating separate bus for insertion of records. I will recommend you to create one bus for one module and write all functions in it. Every module should have its own bus, it is better approach. Now let’s have a look over the bus.

For beginners I again ask you to read the basics of 3-tier architecture first by reading the tutorial 3-Tier Architecture in asp.net using c# in which I went in very detail about the benefits of 3-tier architecture, how to create business layer, how to create data access layer, how to create bus and write function in it and then how to call the function written in bus from .aspx page.

BusFeedback.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLayer;//Donot forget to import this

namespace BusinessLayer
{
public class BusFeedback
{
DbAccess _dbAccess = new DbAccess();
private DataSet _FeedbackDS = new DataSet();
public DataSet FeedbackDS
{
get
{
return _FeedbackDS;
}
set
{
_FeedbackDS = value;
}
}

  //Function to insert data in database, in this example I used MS SQL SERVER    
  public void InsertFeedback(string firstName, string lastName, string feedback)
        {

            try
            {
                string str = "select max(id) as max_id from web_tbl_feedback";
                int maxVal = (_dbAccess.returnint32(str));
                string id = Convert.ToString(maxVal);
                if (string.IsNullOrEmpty(id))
                {
                    maxVal = 1;
                }
                else
                {
                    maxVal++;
                }

                string strInsert = "insert into web_tbl_feedback(id,firstname,lastname,feedback) values('" + maxVal + "','" + firstName + "','" + lastName + "','" + feedback + "')";
                _dbAccess.executeQuery(strInsert);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

}

}

As far as DataAccessLayer namespace and its bus DbAccess.cs is concered, if you are developing new project then you will have to create business layer with its bus busFeedback, DataAccessLayer with its bus DbAccess but if you are using the same project that we developed during previous tutorial then only you have to put BusFeedback in your BusinessLayer folder and that’s it, there is no need to create any DbAccess bus again because one project has only one DbAccess bus but on other end one project may have lot of buses in BusinessLayer folder because those buses depend over modules and DbAccess bus of DataAccessLayer depends over complete project.

So that’s it. Keep in touch as very soon I will write tutorial about deletion and updation of records using 3-tier architecture in asp.net with c#.

Happy Coding!!!

Read more...