Interview Questions | VB.NET Questions | Interview Questions



Get "VB.NET Questions" Category

Sort by:

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

Read more on How to convert the datetime into a string for use in the SQL ‘ statement?…

How to validate that a string is a valid date?

VB.NET

Dim blnValid As Boolean = False
Try
     DateTime.Parse(MyString)
     blnValid = True
Catch
     blnValid = False
End Try

C#

bool blnValid=false;
try
{
     DateTime.Parse(MyString);
     blnValid=true;
}
catch
{
     blnValid=false;
}

Read more on How to validate that a string is a valid date?…