How to close the browser window on button control click

Method 1. This will cause a postback on button click in response to which we will send some script to close the window. VB.NET Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Write(‘<script>window.close();</script>’) End Sub C# private void Button1_Click(object sender, System.EventArgs e) { Response.Write(‘<script>window.close();</script>’); } Method 2. This is preferable since there is no postback involved. Use the Attribute collections i.e use Attributes of button control to add client side JavaScript code VB.NET Button1.Attributes.Add(‘OnClick’, ‘window.close();’) C# Button1.Attributes.Add(‘OnClick’, ‘window.close();’);

What is the difference between authentication and authorization

Authentication is the process of identifying and verifying who the client accessing the server is. For example, if you use Windows authentication and are browsing an ASP.NET page from server — ASP.NET/IIS would automatically use NTLM to authenticate you as SYNCFUSION\user1 (for example). Forms based authentication, then you would use an html based forms page to enter username/password — which would then check a database and authenticate you against the username/password in the database. Authorization is the process of determining whether an authenticated user has access to run a particular page within an ASP.NET web application. Specifically, as an application author decide to grant or deny the authenticated user ‘SYNCFUSION\user1’ access to the admin.aspx page. This could be done either by explicitly granting/denying rights based on the username — or use role based mappings to map authenticated users into roles (for example: an administrator might map ‘SYNCFUSION\user1’ into the ‘Power Users’ role) and then grant/deny access based on role names (allowing a degree of abstraction to separate out your authorization policy).

How to filter distinct records from a normal Select query and display in a web control

‘Select distinct <field> from <table>’.This SELECT statement is used to filter out duplicate results from a query’s output. But sometimes the requirement is to use a Stored Procedure which returns the entire data and display the manipulated distinct data in web server control. To do this use SortedList For using SortedList import namespace System.Collections Stored Procedure Create Procedure GetSuppliers AS SELECT * FROM Suppliers GO <asp:DataList id=’DataList1′ runat=’server’> <ItemTemplate> <%#Container.DataItem%> </ItemTemplate> </asp:DataList> VB.NET Dim ds As New DataSet Dim myda As SqlDataAdapter = New SqlDataAdapter(‘GetSuppliers’, ‘server=localhost;database=Northwind;uid=sa;pwd=;’) myda.SelectCommand.CommandType = CommandType.StoredProcedure myda.Fill(ds, ‘Table’) Dim slist As SortedList = New SortedList Dim dr As DataRow For Each dr In ds.Tables(0).Rows If Not slist.ContainsValue(dr(‘Country’)) Then slist.Add(dr(‘Supplierid’), dr(‘Country’)) End If Next DataList1.DataSource = slist.GetValueList DataList1.DataBind() ’In case of Dropdownlist ’DropDownList1.DataSource = slist.GetValueList ’DropDownList1.DataBind() C# DataSet ds = new DataSet(); SqlDataAdapter myda = new SqlDataAdapter(‘GetSuppliers’, ‘server=localhost;database=Northwind;uid=sa;pwd=;’); myda.SelectCommand.CommandType = CommandType.StoredProcedure; myda.Fill(ds, ‘Table’); SortedList slist = new SortedList(); foreach( DataRow dr in ds.Tables[0].Rows) { if (! slist.ContainsValue(dr[‘Country’])) { slist.Add(dr[‘Supplierid’].ToString (), dr[‘Country’].ToString ()); } } DataList1.DataSource = slist.GetValueList(); DataList1.DataBind(); //In case of DropDownList //DropDownList1.DataSource = slist.GetValueList //DropDownList1.DataBind()

How to insert data in database using Textboxes

<asp:Button id=’btnAdd’ style=’Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 168px’ runat=’server’ Text=’Add Record’></asp:Button> <asp:TextBox id=’txtFirstName’ style=’Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 40px’ runat=’server’></asp:TextBox> <asp:TextBox id=’txtLastName’ style=’Z-INDEX: 103; LEFT: 168px; POSITION: absolute; TOP: 80px’ runat=’server’></asp:TextBox> <asp:TextBox id=’txtDate’ style=’Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 120px’ runat=’server’></asp:TextBox> <asp:Label id=’Label1′ style=’Z-INDEX: 105; LEFT: 56px; POSITION: absolute; TOP: 240px’ runat=’server’></asp:Label> On Page_Load VB.NET if not Page.IsPostBack then ’…. end if C# if(!Page.IsPostBack ) { //… } Use namespaces System.Data.SqlClient, System.Data.SqlTypes On Button Click VB.NET Dim sqlStmt As String Dim conString As String Dim cn As SqlConnection Dim cmd As SqlCommand Dim sqldatenull As SqlDateTime Try sqlStmt = ‘insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date) ‘ conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’ cn = New SqlConnection(conString) cmd = New SqlCommand(sqlStmt, cn) cmd.Parameters.Add(New SqlParameter(‘@FirstName’, SqlDbType.NVarChar, 11)) cmd.Parameters.Add(New SqlParameter(‘@LastName’, SqlDbType.NVarChar, 40)) cmd.Parameters.Add(New SqlParameter(‘@Date’, SqlDbType.DateTime)) sqldatenull = SqlDateTime.Null cmd.Parameters(‘@FirstName’).Value = txtFirstName.Text cmd.Parameters(‘@LastName’).Value = txtLastName.Text If (txtDate.Text = ”) Then cmd.Parameters(‘@Date’).Value = sqldatenull ’cmd.Parameters(‘@Date’).Value = DBNull.Value Else cmd.Parameters(‘@Date’).Value = DateTime.Parse(txtDate.Text) End If cn.Open() cmd.ExecuteNonQuery() Label1.Text = ‘Record Inserted Succesfully’ Catch ex As Exception Label1.Text = ex.Message Finally cn.Close() End Try On Button Click C# string sqlStmt ; string conString ; SqlConnection cn =null; SqlCommand cmd =null; SqlDateTime sqldatenull ; try { sqlStmt = ‘insert into Employees (FirstName,LastName,HireDate) Values (@FirstName,@LastName,@Date) ‘; conString = ‘server=localhost;database=Northwind;uid=sa;pwd=;’; cn = new SqlConnection(conString); cmd = new SqlCommand(sqlStmt, cn); cmd.Parameters.Add(new SqlParameter(‘@FirstName’, SqlDbType.NVarChar, 11)); cmd.Parameters.Add(new SqlParameter(‘@LastName’, SqlDbType.NVarChar, 40)); cmd.Parameters.Add(new SqlParameter(‘@Date’, SqlDbType.DateTime)); sqldatenull = SqlDateTime.Null; cmd.Parameters[‘@FirstName’].Value = txtFirstName.Text; cmd.Parameters[‘@LastName’].Value = txtLastName.Text; if (txtDate.Text == ”) { cmd.Parameters [‘@Date’].Value =sqldatenull ; //cmd.Parameters[‘@Date’].Value = DBNull.Value; } else { cmd.Parameters[‘@Date’].Value = DateTime.Parse(txtDate.Text); } cn.Open(); cmd.ExecuteNonQuery(); Label1.Text = ‘Record Inserted Succesfully’; } catch (Exception ex) { Label1.Text = ex.Message; } finally { cn.Close(); }

What are the pros and cons of using Session or Cache to save intermediate state?

To store in Session: Pros: It’s stored per user session It’s just a handle to a live object, if using InProc If you use StateServer or SQLServer mode, you can access your session state across the webform. (can’t use InProc mode in webform) Cons: If you use StateServer or SQLServer mode, your object must be serializable, and you pay the performance cost of network traffic + serialization cost To use cache: Pros: It’s always stored as just a handle to the live object Can use cache dependencies or other cache features to control its lifetime Can be shared by all users on that web server Cons: It’s per app-domain per web server.