Retrieve id of last record inserted into database

8. March 2010

 

Author : Aamir Hasan
In this article i will show you how to get last inserted record into database to get identity value.@@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions in that they return the last value inserted into the IDENTITY column of a table.SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.


DECLARE @Empoyee TABLE
(
    Id INT IDENTITY(1,1),
    FullName VARCHAR(100),
    Link varchar(200)

)


INSERT INTO  @Empoyee VALUES ('Aamir Hasan','Studentacad.com')

SELECT SCOPE_IDENTITY() AS [Newly Added Identity ID]


 
--OUTPUT
--Newly Added Identity ID
-----------------------------------------
--1

--(1 row(s) affected)

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, SQL 2005 & 2008

Check Window service is running and Check is Stop using .NET (Csharp & VB)

5. March 2010

Author: Aamir Hasan

ServiceController class to control the SimpleService service.
Add reference to  System.ServiceProcess.

Namespace:  System.ServiceProcess
Assembly:  System.ServiceProcess (in System.ServiceProcess.dll)

 

C#

static void Main(string[] args)
{
try
{
ServiceController controller = new ServiceController("SERVICENAME");

if (controller.Status.Equals(ServiceControllerStatus.Running)
&& controller.CanStop)
{
controller.Stop();
Console.WriteLine("Service Stopped");
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

VB.NET

Shared Sub Main(ByVal args() As String)
Try
Dim
controller As New ServiceController("SERVICENAME")

If controller.Status.Equals(ServiceControllerStatus.Running)_
          AndAlso controller.CanStop Then
controller.Stop()
Console.WriteLine("Service Stopped")
End If
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub



Reference http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, .NET

How To Remove WSDL from webservices

5. March 2010

Author Aamir Hasan

Many times people ask  me about, how to disable WSDL information from everyone, because every one can see it if any one know the page name *.asmx.So

You have to do, add these line of code to your web config file

<? xmlversion="1.0" encoding="utf-8"?>
<configuration>
<system.web>
                <webServices>     
                     <protocols>       
                           <remove name="Documentation"/>      
                     </protocols> 
              </webServices>
</system.web>
</configuration>

 

 

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,

How to Track Session using asp.net Csharp

4. March 2010

Author: Aamir Hasan



Create StateCollection Class;

public class StateCollection
{
private string _sessionId;
private string _path;
private string _username;
private int _pageview = 1;

public string SessionID { get { return _sessionId; } set { _sessionId = value; } }
public string Path { get { return _path; } set { _path = value; } }
public string Username { get { return _username; } set { _username = value; } }
public int PageView { get { return _pageview; } set { _pageview = value; } }
}



Create SessionTrack Class;

public static class SessionTrack
{
public static ArrayList States = new ArrayList();

static SessionTrack() { }

public static void Add(StateCollection state)
{
int StateIndex = Index(state);
if (StateIndex < 0)
States.Add(state);
else
{
int PageView = ((StateCollection)States[StateIndex]).PageView;
States[StateIndex] = state;
((StateCollection)States[StateIndex]).PageView = PageView + 1;
}
}

public static void Remove(StateCollection state)
{
int StateIndex = Index(state);
if (StateIndex >= 0)
States.RemoveAt(StateIndex);
}

private static int Index(StateCollection state)
{
for (int i = 0; i < States.Count; i++)
if (((StateCollection)States[i]).SessionID == state.SessionID)
return i;
return -1;
}
}



Create default.aspx Page;

Copy Paste Label and Repeater;

<asp:Label ID="LSTotal" runat="server" Text="Visitors: "></asp:Label>
<hr />
<table>
<tr>
<td style="width:200px;">Session ID</td>
<td style="width:100px;">User name</td>
<td style="width:100px;">Page View</td>
<td style="width:300px;">Path</td>
</tr>

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"SessionID") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Username")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "PageView")%></td>
<td><%# DataBinder.Eval(Container.DataItem, "Path")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>



In default.aspx.cs Page Load

protected void Page_Load(object sender, EventArgs e)
{
LSTotal.Text += SessionTrack.States.Count;

Repeater1.DataSource = SessionTrack.States;
Repeater1.DataBind();
}



Create Global.aspx Page

replace this function with Session_End if it is Empty
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
SessionTrack.Remove(new StateCollection() { SessionID = Session.SessionID });
}


now any one who is connected to Your site will be seen in this page and also you can track of the any page.
this is how simple you can track session in your web admin site.

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net , ,

how to Access dropdownlist inside the gridview

1. March 2010

Author: Aamir Hasan

how you can embed a DropDownList inside the GridView control. You can check out the article at  If you look closely you will notice that all the DropDownList inside the GridView contains the same information. In this article I will demonstrate that how you can have DropDownList inside the GridView exposing different information.dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.

In such a case we just need to access the controls that has fired the event and this can be done just by using this line

 

GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;

 

 

 < asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" OnDataBound="GV_DataBound" 
                     OnRowDataBound="GV_RowDataBound"> 
                     < Columns> 
                         < asp:TemplateField> 
                             <ItemTemplate> 
                                 <asp:Label ID="lblId" runat="server" Text='<%#Eval("id") %>'></asp:Label> 
                             </ItemTemplate> 
                         </asp:TemplateField> 
                         <asp:TemplateField> 
                             <ItemTemplate> 
                                 <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label> 
                             </ItemTemplate> 
                         </asp:TemplateField> 
                         <asp:TemplateField> 
                             <ItemTemplate> 
                                 <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> 
                                 <asp:Label ID="lblOriginalPrice" runat="server" Text='<%#Eval("price") %>' Visible="false"></asp:Label> 
                             </ItemTemplate> 
                         </asp:TemplateField> 
                         <asp:TemplateField> 
                             <ItemTemplate> 
                                 <asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQty_SelectedIndexChanged"> 
                                     <asp:ListItem Text="1" Value="1"></asp:ListItem> 
                                     <asp:ListItem Text="2" Value="2"></asp:ListItem> 
                                     <asp:ListItem Text="3" Value="3"></asp:ListItem> 
                                     <asp:ListItem Text="4" Value="4"></asp:ListItem> 
                                     <asp:ListItem Text="5" Value="5"></asp:ListItem> 
                                     <asp:ListItem Text="6" Value="6"></asp:ListItem> 
                                     <asp:ListItem Text="7" Value="7"></asp:ListItem> 
                                     <asp:ListItem Text="8" Value="8"></asp:ListItem> 
                                 </asp:DropDownList> 
                             </ItemTemplate> 
                         </asp:TemplateField> 
                     </Columns> 
                 </asp:GridView>

protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e) 
     { 
         GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent; 
         DropDownList ddlQty = (DropDownList)gr.FindControl("ddlQty"); 
         if (ddlQty != null) 
         { 
              
                 Label lblPrice = (Label)gr.FindControl("lblPrice"); 
                 Label lblOriginalPrice = (Label)gr.FindControl("lblOriginalPrice"); 
                 int price = Convert.ToInt32(lblOriginalPrice.Text); 
                 int NewPrice = price * Convert.ToInt16(ddlQty.SelectedValue); 
                 lblPrice.Text = Convert.ToString(NewPrice);   
            
         } 
     }

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,

User Name: Guest

Your Ip: 38.107.191.111
Time: