Search This Blog

Tuesday, March 19, 2013

How can use a Session variable from a .cs file.


How can use a Session variable from a .cs file.

I have a Session variable defined in login time in application and ues this session on another .cs file (which doesn't have a corresponding .aspx file), error accour

on page : "The name 'Session' does not exist in the current context".

Question is that how can I access the Session variable on cs file ???

namespace : using System.Web.SessionState
Interface : IRequiresSessionState


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;

///IRequiresSessionState enable session in Class file

public class Class1:IRequiresSessionState
{
    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void getSession(string key)
    {
        if (HttpContext.Current.Session[key] != null)
        {
            string sessionID= HttpContext.Current.Session[key].ToString();
        }
    }
}

How to Retrieve TOP and BOTTOM Rows Together using T-SQL



Table Design:

CREATE TABLE [dbo].[student](

    [FirstName] [nvarchar](50) NULL

    [ID] [int] NULL,

    [Technology] [nvarchar](50) NULL

) ON [PRIMARY]

 

GO

select * from student

FirstName
ID
Technology
Ravi Sharma
1
DotNet
Ravi
3
DotNet
Avdesh
4
DotNet
CP
5
JAVA
ROHIT
6
ASP
AMIT
7
JS
RITU
8
VB
ATUL
9
C
GINI
10
SQL


1st Query:

SELECT A.* FROM (SELECT TOP 1 * FROM student ORDER BY ID) A

UNION ALL

SELECT B.* FROM (SELECT TOP 1 * FROM student ORDER BY ID DESC) B

Query Output:

FirstName
ID
Technology
Ravi Sharma
1
DotNet
GINI
10
SQL


2nd Query:


SELECT * FROM student WHERE ID IN (SELECT TOP 1 MIN(ID) ID FROM student

UNION ALL

SELECT TOP 1 MAX(ID) ID FROM student)

Query Output:

FirstName
ID
Technology
Ravi Sharma
1
DotNet
GINI
10
SQL

 

Monday, March 18, 2013

How can insert HINDI TEXT and multilingual in SQL -2008


In SQL
In SQL create a table with required data entry. And since you want to add Hindi in a column the data type should be NVARCHAR. The columns TestName are NVARCHAR data type.

In aspx page :
In the aspx page, draw a textbox and button control. Then write the following code for saving the data to a SQL Database.Write this code for the button's Click event:

Insert into hindi_Test(TestName) values(N'आइए जानते हैं कंगारुओं को कब-कब मिली करारी हार')

INSERT INTO table1 VALUES (N’your multilingual value here’)



In aspx page for SQL Stored Procedures:
 public int INS_TestData(string TestName)
        {
            dbSqlCommand = new SqlCommand();
            dbSqlCommand.Connection = dbSqlconnection;
            if (dbSqlconnection.State == ConnectionState.Closed)
                dbSqlconnection.Open();
            dbSqlCommand.CommandType = CommandType.StoredProcedure;
            dbSqlCommand.CommandText = "TestHIndiData";
            dbSqlCommand.Parameters.Add("@TestName", SqlDbType.NVarChar).Value = TestName;
            System.Data.SqlClient.SqlParameter pRowsAffected = new SqlParameter("@RowsAffected", System.Data.SqlDbType.Int);
            pRowsAffected.Direction = System.Data.ParameterDirection.Output;
            dbSqlCommand.Parameters.Add(pRowsAffected);
            try
            {
                dbSqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                return -1;
            }
            return Convert.ToInt32(pRowsAffected.Value);
        }

SQL Stored Procedures for insert hindi text in table sql 2008:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TestHIndiData]
@TestName as nvarchar(50),
@RowsAffected int output
AS
BEGIN
INSERT INTO  dbo.hindi_Test(TestName) VALUES (@TestName)
Set @RowsAffected =@@identity
 END

Friday, March 15, 2013

Basic Threading in C#


Basic Threading in C#:

Threading make your application very smoother. A single thread in a C# application, is an independent execution path that can run simultaneously with the main application thread. C# supports multithreading. And to use the threading namespace, you can directly call it from System.Threading.

Example :

using System;
using System.Threading;

class Program
{
    static void Main()
    {
Thread thread1 = new Thread(new ThreadStart(run));
Thread thread2 = new Thread(new ThreadStart(stop));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
    }

    static void run()
    {
Thread.Sleep(100);
Console.WriteLine('run');
    }

    static void stop()
    {
Thread.Sleep(1000);
Console.WriteLine('stop');
    }
}

Code Output :
(The threads terminate after 0.1 and 1.0 seconds.)
run
stop

public void SMSThreadFunction()
        {
            try
            {
                if (Convert.ToString(Session["customer_number"]) == string.Empty || Convert.ToString(Session["customer_number"]) == "" || Convert.ToString(Session["customer_number"]) == null)
                {
                    sent_message_ACL(Convert.ToString(Session["MOBILE"]), Convert.ToString(Session["RET_MESSAGE"]));
                    sendsms(Convert.ToString(Session["MOBILE"]), Convert.ToString(Session["RET_MESSAGE"]));
                    LBL_MESS.Text = Convert.ToString(Session["RET_MESSAGE"]);

                }
                else if (Convert.ToString(Session["customer_number"]) != string.Empty || Convert.ToString(Session["customer_number"]) != "")
                {
                    sent_message_ACL(Convert.ToString(Session["MOBILE"]), Convert.ToString(Session["RET_MESSAGE"]));
                    sent_message_ACL(Convert.ToString(Session["customer_number"]), Convert.ToString(Session["Customer_MSG"]));
                    sendsms(Convert.ToString(Session["MOBILE"]), Convert.ToString(Session["RET_MESSAGE"]));
                    sendsms(Convert.ToString(Session["customer_number"]), Convert.ToString(Session["Customer_MSG"]));

                    LBL_MESS.Text = Convert.ToString(Session["RET_MESSAGE"]) + "</br>" + Convert.ToString(Session["Customer_MSG"]);


                }
            }
            catch (Exception ex)
            {
               
            }
        }


Call Function in Threading:

 if (Convert.ToString(Session["UserWeb"]) == "Success")
                {

                    Response.Redirect("../charges.aspx?userid=" + Convert.ToInt32(Session["userid"]));

                }
                else
                {
                    Thread thread = new Thread(new ThreadStart(SMSThreadFunction));
                    thread.Start();
                }

Saturday, March 9, 2013

How can publish asp.net website in vs 2008


Publish a Web site project:


1.       On the Build menu, click Publish Web Site.
2.       In the Publish Web Site dialog box.
3.       We have 4 check boxes in dialog window.


a). Allow this precompiled site to be updatable.
Classic precompilation: *.aspx pages are not precompiled, but .cs classes and code-behind files are precompiled.
To be able to change.aspx files after publishing the Web site (without recompiling them) When this check box is selected, it specifies that the content of .aspx pages are not compiled into an assembly - the markup is left as is, allowing you to change HTML and client-side functionality after precompiling the Web site. Alternatively you can use
-u option with the aspnet_compiler.exe command line tool to achieve same result.

b). Use fixed naming and single page assemblies.
If you want to be able to update individual pages by recompiling them, select Use fixed naming and single page assemblies.
Fixed named assemblies provide flexiblity in page level updates. Since each page will have its own named assembly (instead of a random name) it is easy to update the application by just updating the required assembly.
 batch compilation is turned off for precompilation. (Themes and skin files, along with other folders that do not contain pages or user controls, will continue to be compiled to a single assembly.)
Disadvantage is the large number of assemblies (difficult to maintain) and also slight performance hit would be there.

c). Emit debug information.
We want to be able to debug in the published Web site, select Emit debug information.

d). Enable strong naming on precompiled assemblies.
if you want to allow the assemblies to be called by partially trusted code, select Mark assemblies with AllowPartiallyTrustedCallerAttribute (APTCA).

Wednesday, March 6, 2013

Browser Information by asp.net code


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>chandra prakash ask-dotnet.blogspot.com</title>
</head>
<body>

 <p><font face="Arial" size="3"><b>Browser Information by asp.net code</b></font></p>
 <hr align="left" color="#000000" style="width: 44%">
 <div align="left">
  <table border="1"  width="44%" style=color="#000000">
    <tr>
      <td width="34%"><font face="Verdana" size="2">Browser Type</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Browser %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Version</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Version %></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">Major Version</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.MajorVersion%></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">Minor Version</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.MinorVersion%></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Is Beta</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Beta%></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">Crawler</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%=Request.Browser.Crawler%></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">AOL</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.AOL%></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Is WIN16</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Win16%></font></td>
    </tr>
   
    <tr>
      <td width="34%"><font face="Verdana" size="2">Is Win32</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Win32%></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">Supports Frames</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%=Request.Browser.Frames%></font></td>
    </tr>
     <tr>
      <td width="34%"><font face="Verdana" size="2">Supports Tables</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.Tables%></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Supports Cookies</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%=Request.Browser.Cookies%></font></td>
    </tr>
   <tr>
      <td width="34%"><font face="Verdana" size="2">Java Script</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.JavaScript %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">ActiveX Control</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.ActiveXControls %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Java Script</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.JavaScript %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">Java Applet</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.JavaApplets %></font></td>
    </tr>
    <tr>
      <td width="34%"><font face="Verdana" size="2">VB Script</font></td>
      <td width="4%"><font face="Verdana" size="2">:</font></td>
      <td width="66%"><font face="Verdana" size="2"><%= Request.Browser.VBScript %></font></td>
    </tr>
  </table>
 </div>

 </body>
</html>

OUTPUT :