Friday, August 17, 2012

Get Records between two dates in MS SQL

Here query show how to select record between two dates in sql server.
select * from ContentMst
where c_date> = '2012-06-01' and c_date <= '2012-07-01'
order by c_date desc

How to insert record from one database to other database server

Here example show i am retrieving record from one server(DBServer2) and inserting in other server(DBServer1).
insert into Category(DBServer1)
select * from [DBServer2].[DatabaeName].[dbo].Category
Note: Both database server should be linq server otherwise it will give error.

Wednesday, July 4, 2012

How to create a hyperlink or any other control dynamically and add to this in panel control

 Here i am show how can you create hyperlink dynamically and add to it in panel control.  
 Here we can also create line break dynamically.  
   
 Take a asp.net panel in your .aspx page as given below:  
   
 <asp:Panel ID="Panel2" runat="server" CssClass="applycss">  
 </asp:Panel>  
   
 .cs code to create HyperLink dynamically and add to panel.  
        
 HyperLink hyprlink = new HyperLink();  
 hyprlink.Text = "Click here to subscribe.";  
 hyprlink.CssClass = "Ima12";  
 hyprlink.NavigateUrl = "http://yourdomain/SubApplication/?key=passparameter&rs=2";  
 Panel2.Controls.Add(hyprlink);  
 Panel2.Controls.Add(new LiteralControl("<br />"));  
   
 Regards  
 Santosh Singh  

Thursday, April 12, 2012

How to create or implement Remember me Login Page asp.net and c#


 Login page source code in asp.net and c#  
 Hi,as you have seen various example above,please see one other example for login page using  
 Here i have implement Remember me on Login Page using checkbox. now i am going to explain  
 how to use Remember Me checkbox and login page code using asp.net and c# net.When you checked Remember me  
 checkbox it will maintain username and password for website user using Cookies.  
 When user click login button that will stores the username and password in the cookie.  
 first determine if the remember me checkbox is checked or not, if it is checked then store the username and   
 password in the cookie for the 1 month and if not checked then set the expiration date to 1 day in else condition  
 to destroy the cookie.  
 protected void Page_Load(object sender, EventArgs e)  
 {  
      if (!Page.IsPostBack)  
      {  
           if (Request.Cookies["UserName"] != null)  
                txtUserName.Text = Request.Cookies["UserName"].Value;  
           if (Request.Cookies["Password"] != null)  
                txtPassword.Text = Request.Cookies["Password"].Value;  
           if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null)  
                chkRember.Checked = true;   
      }  
 }  
 protected void btnLogin_Click(object sender, EventArgs e)  
 {  
       string stringconnection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;  
       MySqlConnection connection = new MySqlConnection(stringconnection);  
      try  
      {   
           int flag = 0;  
           if (chkRember.Checked == true)  
           {  
                Response.Cookies["UserName"].Value = txtUserName.Text;  
                Response.Cookies["Password"].Value = txtPassword.Text;  
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);  
                Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(1);  
           }  
           else  
           {  
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(-1);  
                Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(-1);  
           }   
           MySqlCommand com = new MySqlCommand("select * from tbl_user_details", connection);  
           connection.Open();  
           if (connection.State == ConnectionState.Open)  
           {  
                MySqlDataReader objSqlDataReader;  
                objSqlDataReader = com.ExecuteReader();  
                while (objSqlDataReader.Read())  
                {  
                     if (objSqlDataReader[2].ToString().Equals(txtUserName.Text) && objSqlDataReader[5].ToString().Equals(txtPassword.Text))  
                     {  
                          flag = 1;  
                          Session["UserName"] = txtUserName.Text;  
                          Session["UserType"] = objSqlDataReader[8].ToString();  
                          Response.Redirect("ContactUs.aspx", false);  
                     }  
                     else  
                     {  
                          ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");  
                     }  
                }  
           }  
      }  
      catch (Exception ex)  
      {  
           lblMsg.Text = ex.ToString();  
      }  
      finally   
      {  
           connection.Close();  
      }  
 }  
 Regard  
 Santosh Singh  

Wednesday, March 7, 2012

How to improve asp.net Web application performance

 For application performance you need to some r & d on your query mean stored procedure  
 see their retrieval cost and find where it is taking too much time to retriev the data  
 and do some action on that table.  
 For eg.   
 1.You should be use index whereever required  
 2.Use joining if required otherwise remove unnesseary join.  
 3.Select only required field from table.  
 4.you can using caching  
 5.Create index for the table field which you are frequently using in where clause.  

Tuesday, March 6, 2012

How to get label or textbox control from .aspx page in .ascx user control page

 How to access controls from .aspx in .ascx user control page  
 Here i am showing a simple example how can we access controls from aspx in .ascx page  
 First i have created a WebUserControl.ascx user control page which contain a button.  
 WebUserControl.ascx page  
 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>  
 <table>  
   <tr>  
     <td>  
       <asp:Button ID="btn_addUC" runat="server" Text="Add" OnClick="btn_addUC_Click" />  
     </td>  
   </tr>  
 </table>  
 Write below code in code behind   
 WebUserControl.ascx.cs page  
 protected void btn_addUC_Click(object sender, EventArgs e)  
 {  
      Button btnSender = (Button)sender;  
      Page parentPage = btnSender.Page;      
      TextBox txt = (TextBox)parentPage.FindControl("txtName");  
      Label lbl = (Label)parentPage.FindControl("lblNAme");  
      lbl.Text = txt.Text;  
 }  
 Take a another test_page.aspx page and register the above user control in it.as given below:  
 test_page.aspx page  
 <form id="form1" runat="server">  
      <div>  
           <uc1:WebUserControl runat="server" ID="WebUserControl" />  
      </div>  
      <div>  
           <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />  
           <asp:Label ID="lblNAme" runat="server" Text="Label"></asp:Label>  
      </div>  
 </form>  
      Run and test the application.  
 Regards  
 Santosh Singh  

Monday, February 27, 2012

How to detect MSISDN from browser headers

        How to detect MSISDN from browser headers  
        Things to note be remember that:  
        1.If the user has come to wap portal on WiFi then you will not receive msisdn.  
        2.User mobile operator has to support the passing of the msisdn in the HTTP      headers.  
                  
        Below is simple code to find msisdn from header.  
                  
        string[] strHeader = Request.Headers.AllKeys;  
       string strMSISDN="";  
       for (int i = 0; i < strHeader.Length; i++)  
       {  
         if (strHeader[i].Contains("msisdn") || strHeader[i].ToLower().StartsWith("msisdn", StringComparison.CurrentCultureIgnoreCase) ||strHeader[i].ToLower().EndsWith("msisdn", StringComparison.CurrentCultureIgnoreCase) ||strHeader[i].ToLower().StartsWith("mdn", StringComparison.CurrentCultureIgnoreCase) ||strHeader[i].ToLower().EndsWith("mdn", StringComparison.CurrentCultureIgnoreCase) ||)  
         {  
           strMSISDN = Request.Headers[strHeader[i]];  
   
           if (strMSISDN.Length >= 15)  
           {  
             for (int j = 0; j < strHeader.Length; j++)  
             {  
               if (strHeader[j].ToLower().StartsWith("mdn", StringComparison.CurrentCultureIgnoreCase) || strHeader[j].ToLower().EndsWith("mdn", StringComparison.CurrentCultureIgnoreCase))  
               {  
                 strMSISDN = Request.Headers[strHeader[j]];  
                 break;  
               }  
             }  
           }  
           else  
           {  
             break;  
           }  
         }  
       }  
   
 Thanks & Regards  
 Santosh  

Rain Water Harvesting

Wednesday, February 22, 2012

Create HTTP request and receive response using asp.net C#

 Create HTTP request and receive response using asp.net C#   
       1.Simple http request  
        Uri objurl = new Uri(url);  
       WebRequest objWebRequest = WebRequest.Create(objurl);  
       WebResponse objWebResponse = objWebRequest.GetResponse();  
       Stream objstream = objWebResponse.GetResponseStream();  
       StreamReader objStreamReader = new StreamReader(objstream);  
       string strHtml = objStreamReader.ReadToEnd();  
        2.HTTP Post request and response  
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://clinet-url.com/sub/Activate");        
       req.Method = "POST";  
       req.ContentType = "application/x-www-form-urlencoded";  
       req.Credentials = CredentialCache.DefaultNetworkCredentials;  
       req.ClientCertificates.Add(clint_certificate);  
       strNewReqValue = strFormValues + "&mssdn=" + msisdn + "&srv=" + Service + "&user=username";  
       req.ContentLength = strNewReqValue.Length;  
       StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);  
       stOut.Write(strNewReqValue);  
       stOut.Close();  
       StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());  
       Res = stIn.ReadToEnd();  
       Response = Res.ToString();  
       stIn.Close();  

Tuesday, February 21, 2012

How to use stored Procedure with output parameters with ASP.NET,c#

 How to retrieve store procedure output parameter value?I have given a simple code example.  
 I have created a store procedure with output parameter. After that I get it from code behind page of  
 asp.net and stored it a variable.    
 private void GetInfo()  
   {  
     DALUtility objDALUtility = null;  
     SqlConnection con = null;  
     SqlCommand cmd = null;  
     SqlDataAdapter da = null;  
     try  
     {  
       objDALUtility = new DBUtility(ConfigurationManager.AppSettings["myconString"]);  
       con = objDALUtility.GetDBConnection();  
       con.Open();  
       cmd = new SqlCommand();  
       cmd.Connection = con;  
       cmd.CommandType = CommandType.StoredProcedure;  
       cmd.CommandTimeout = 500;  
       cmd.CommandText = "usp_MyInfo";  
       cmd.Parameters.AddWithValue("@Id", strId);  
       cmd.Parameters.AddWithValue("@empName", Convert.ToString(Session["Name"]));        
       cmd.Parameters.Add("@TotalCount", SqlDbType.Int).Direction = ParameterDirection.Output;  
       da = new SqlDataAdapter(cmd);  
       DataSet objDS = new DataSet("MyInfo");  
       da.Fill(objDS);  
       con.Close();  
       int Total = Convert.ToInt32(cmd.Parameters["@TotalCount"].Value);  
     }  
   }  
 Stored Procedure  
 CREATE Proc [dbo].[usp_MyInfo]  
  @Id int,            
  @Name varchar(50) = null,      
  @Total int = 0 OUTPUT  
 AS          
 SET @Total = (Select COUNT(Distinct tbl_Logs.ContentId) From tbl_Logs with(nolock)         
  Inner Join tbl_Logs_details with(nolock) on tbl_Logs_details.ContentId = tbl_Logs.ContentId  
  Where id = @id and tbl_Logs_details.ContentId = 2   
 Select @Total as Count