Interview Tips Interview Tips, Interview Questions and Answers

18Sep/110

Strongly Typed Data Controls in asp.net 5

Strongly Typed Data Controls

The next release of ASP.NET provides the ability to enable strongly-typed data templates.  Specifically, we’ve added the ability to declare what type of data a control is going to be bound to, by way of a new “ModelType” property on data controls.  Setting this property will cause two new typed variables to be generated in the scope of the data-bound template expressions: Item and BindItem.

Developers can use these variables in data-binding expressions and get full Intellisense and compile-time checking support.  For example, below we’ve set the ModelType on an <asp:repeater> control to be a “Customer” object.  Once we do this we can switch from using Eval(“FirstName”) to instead use Item.FirstName to reference the property.

We get full Visual Studio code intellisense when we do so:

 

For 2-way data-binding expressions, we can also now use the BindItem variable and get the same strongly-typed benefits:

<asp:FormView ID="editCustomer" runat="server">

    <EditItemTemplate>

        <div>

            First Name:

            <asp:TextBox ID="firstName" Text='<%# BindItem.FirstName %>' runat="server" />

        </div>

        <div>

            Last Name:

            <asp:TextBox ID="lastName" Text='<%# BindItem.LastName %>' runat="server" />

        </div>

        <asp:Button runat="server" CommandName="Update" />

    </EditItemTemplate>

</asp:FormView>

11Mar/110

How do we connect to SQL SERVER, which namespace do we use in .NET

elow is the code, after the code we will try to understand the same in a more detailed manner. For this sample, we will also need a SQL Table setup, which I have imported, using the DTS wizard.

Private Sub LoadData()
‘ note :- with and end with makes your code more readable
Dim strConnectionString As String
Dim objConnection As New SqlConnection
Dim objCommand As New SqlCommand
Dim objReader As SqlDataReader
Try
‘ this gets the connectionstring from the app.config file.
‘ note if this gives error see where the MDB file is stored in your pc and point to thastrConnectionString = AppSettings.Item(“ConnectionString”)
‘ take the connectiostring and initialize the connection object
With objConnection
.ConnectionString = strConnectionString
.Open()
End With
objCommand = New SqlCommand(“Select FirstName from Employees”)
With objCommand
.Connection = objConnection
objReader = .ExecuteReader()
End With
‘ looping through the reader to fill the list box
Do While objReader.Read()
lstData.Items.Add(objReader.Item(“FirstName”))
Loop
Catch ex As Exception
Throw ex
Finally
objConnection.Close()
End Try