How to convert the datetime into a string for use in the SQL ‘ statement

<asp:label id=’Label2′ runat=’server’>Select a culture: </asp:label> <asp:dropdownlist id=’ddlCulture’ runat=’server’ autopostback=’True’></asp:dropdownlist> <P></P> <asp:label id=’Label3′ runat=’server’>DateTime in Selected Culture</asp:label> <asp:textbox id=’TextBox1′ runat=’server’></asp:textbox> <p> <asp:label id=’Label1′ runat=’server’></asp:label> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then Dim cInfo As CultureInfo For Each cInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures) ddlCulture.Items.Add(cInfo.Name) Next End If End Sub Private Sub ddlCulture_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCulture.SelectedIndexChanged ’ Get a CultureInfo object based on culture selection in dropdownlist Dim cInfo As CultureInfo = New CultureInfo(ddlCulture.SelectedItem.Text) ’ Get a CultureInfo object based on Invariant culture Dim cInfoNeutral As CultureInfo = New CultureInfo(”) ’ Display the datetime based on the formatting of the selected culture TextBox1.Text = Convert.ToString(Now, cInfo.DateTimeFormat) ’ Create a DateTime variable to hold the Invariant time Dim dt As DateTime dt = Convert.ToDateTime(TextBox1.Text, cInfo.DateTimeFormat) ’Convert the datetime into a string for use in the SQL statement Label1.Text = ‘… WHERE ([Date] < ’’ & _ Convert.ToString(dt, cInfoNeutral.DateTimeFormat) & ‘’)’ End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { foreach(CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { ddlCulture.Items.Add(cInfo.Name); } } } private void ddlCulture_SelectedIndexChanged(object sender, System.EventArgs e) { // Get a CultureInfo object based on culture selection in dropdownlist CultureInfo cInfo = new CultureInfo(ddlCulture.SelectedItem.Text); // Get a CultureInfo object based on Invariant culture CultureInfo cInfoNeutral = new CultureInfo(”); // Display the datetime based on the formatting of the selected culture TextBox1.Text = Convert.ToString(DateTime.Now , cInfo.DateTimeFormat); // Create a DateTime variable to hold the Invariant time DateTime dt ; dt = Convert.ToDateTime(TextBox1.Text, cInfo.DateTimeFormat); //Convert the datetime into a string for use in the SQL statement Label1.Text = ‘… WHERE ([Date] < ’’ + Convert.ToString(dt, cInfoNeutral.DateTimeFormat) + ‘’)’; }

How To work with TimeSpan Class

VB.NET Dim adate As DateTime = DateTime.Parse(’06/24/2003′) Dim bdate As DateTime = DateTime.Parse(’06/28/2003′) Dim ts As New TimeSpan(bdate.Ticks – adate.Ticks) Response.Write(ts.TotalDays & ‘<br>’) Response.Write(ts.TotalHours & ‘:’ & ts.TotalMinutes & ‘:’ & ts.TotalSeconds & ‘:’ & ts.TotalMilliseconds) C# DateTime adate = DateTime.Parse(’06/24/2003′); DateTime bdate = DateTime.Parse(’06/28/2003′); TimeSpan ts = new TimeSpan (bdate.Ticks – adate.Ticks); Response.Write(ts.TotalDays.ToString () + ‘<br>’); Response.Write(ts.TotalHours.ToString() + ‘:’ + ts.TotalMinutes.ToString() + ‘:’ + ts.TotalSeconds.ToString() + ‘:’ + ts.TotalMilliseconds.ToString() );

Can I specify ItemStyle to a Repeater Control

The Repeater class is not derived from the WebControl class, like the DataGrid and DataList. Therefore, the Repeater lacks the stylistic properties common to both the DataGrid and DataList. What this boils down to is that if you want to format the data displayed in the Repeater, you must do so in the HTML markup. i.e using <b>/<u>.. tags

How to implement sorting using Repeater

<asp:Repeater ID=’Repeater1′ Runat=’server’ EnableViewState=’False’> <ItemTemplate> <table width=’100%’ cellspacing=’0′ cellpadding=’0′> <tr> <td width=25%><%# DataBinder.Eval(Container.DataItem, ‘Employeeid’) %></td> <td width=25% ><%# DataBinder.Eval(Container.DataItem, ‘FirstName’) %></td> <td width=25% ><%# DataBinder.Eval(Container.DataItem, ‘LastName’) %></td> <td width=25% ><%# DataBinder.Eval(Container.DataItem, ‘Title’) %></td> </tr> </table> </ItemTemplate> <HeaderTemplate> <table width=’100%’ cellspacing=’0′ cellpadding=’0′> <tr> <td width=25%> <asp:LinkButton ID=’lnkEmployeeid’ Runat=’server’ OnClick=’SortEmployeeIdClick’>Employeeid</asp:LinkButton> </td> <td width=25%> <asp:LinkButton ID=’lnkFirstName’ Runat=’server’ OnClick=’SortFirstNameClick’>FirstName</asp:LinkButton> </td> <td width=25%> <asp:LinkButton ID=’lnkLastName’ Runat=’server’ OnClick=’SortLastNameClick’>LastName</asp:LinkButton> </td> <td width=25% > <asp:LinkButton ID=’lnkTitle’ Runat=’server’ OnClick=’SortTitleClick’>Title</asp:LinkButton> </td> </tr> </table> </HeaderTemplate> </asp:Repeater> VB.NET Dim SortField As String Dim myconnection As SqlConnection Dim myda As SqlDataAdapter Dim ds As DataSet Dim strConn As String Dim SQLStmt As String = ‘Select * from Employees ‘ Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’ Put user code to initialize the page here BindData() End Sub ’Page_Load Sub BindData() strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’ myconnection = New SqlConnection(strConn) myda = New SqlDataAdapter(SQLStmt, myconnection) ds = New DataSet myda.Fill(ds, ‘AllTables’) Repeater1.DataSource = ds Repeater1.DataBind() End Sub ’BindData Sub SortData(ByVal SortExpression As String) If ViewState(‘SortOrder’) Is Nothing Then ViewState(‘SortOrder’) = ‘ ASC’ Else If ViewState(‘SortOrder’).ToString() = ‘ ASC’ Then ViewState(‘SortOrder’) = ‘ DESC’ Else ViewState(‘SortOrder’) = ‘ ASC’ End If End If SQLStmt = SQLStmt + ‘ ORDER BY ‘ + SortExpression.ToString() + ‘ ‘ + ViewState(‘SortOrder’) BindData() End Sub ’SortData Protected Sub SortEmployeeIdClick(ByVal sender As Object, ByVal e As EventArgs) SortField = ‘EmployeeId’ SortData(SortField) End Sub ’SortEmployeeIdClick Protected Sub SortFirstNameClick(ByVal sender As Object, ByVal e As EventArgs) SortField = ‘FirstName’ SortData(SortField) End Sub ’SortFirstNameClick Protected Sub SortLastNameClick(ByVal sender As Object, ByVal e As EventArgs) SortField = ‘LastName’ SortData(SortField) End Sub ’SortLastNameClick Protected Sub SortTitleClick(ByVal sender As Object, ByVal e As EventArgs) SortField = ‘Title’ SortData(SortField) End Sub ’SortTitleClick C# string SortField; SqlConnection myconnection ; SqlDataAdapter myda ; DataSet ds ; String strConn ; string SQLStmt= ‘Select * from Employees ‘; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here BindData(); } void BindData() { strConn = ‘Server=localhost;uid=sa;password=;database=northwind;’; myconnection =new SqlConnection(strConn); myda = new SqlDataAdapter(SQLStmt, myconnection); ds = new DataSet(); myda.Fill(ds, ‘AllTables’); Repeater1.DataSource = ds; Repeater1.DataBind(); } void SortData(string SortExpression) { if (ViewState[‘SortOrder’] ==null) { ViewState[‘SortOrder’] = ‘ ASC’; } else if (ViewState[‘SortOrder’].ToString () == ‘ ASC’ ) { ViewState[‘SortOrder’] = ‘ DESC’; } else { ViewState[‘SortOrder’] = ‘ ASC’; } SQLStmt = SQLStmt + ‘ ORDER BY ‘ + SortExpression.ToString () + ‘ ‘ + ViewState[‘SortOrder’]; BindData(); } protected void SortEmployeeIdClick(object sender ,EventArgs e ) { SortField = ‘EmployeeId’; SortData (SortField); } protected void SortFirstNameClick(object sender ,EventArgs e ) { SortField = ‘FirstName’; SortData (SortField); } protected void SortLastNameClick(object sender ,EventArgs e ) { SortField = ‘LastName’; SortData (SortField); } protected void SortTitleClick(object sender ,EventArgs e ) { SortField = ‘Title’; SortData (SortField); }

How to change the row color of the Repeater based on some condition

<asp:Repeater ID=’Repeater1′ Runat=’server’ EnableViewState=’False’> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr <%# FormatColorRow(DataBinder.Eval(Container.DataItem,’UnitPrice’).ToString()) %> > <td > <%# DataBinder.Eval(Container.DataItem,’UnitPrice’).ToString() %> </td> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> VB.NET Protected Function FormatColorRow(strUnitPrice As String) As String Dim unitprice As Double = Double.Parse(strUnitPrice) If unitprice <= 15 Then Return ‘style=’backGround-color:red’’ Else Return ‘style=’backGround-color:green’’ End If End Function ’FormatColorRow C# protected string FormatColorRow(string strUnitPrice) { double unitprice =double.Parse ( strUnitPrice); if ( unitprice <= 15) { return ‘style=’backGround-color:red’’; } else { return ‘style=’backGround-color:green’’; } }