Hi all,

I am new to asp.net programming and i am using asp.net calender control
the code tht i am using is

Protected Sub btn_dob_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_dob.Click
        Try
            If (txt_dob.Text.Trim() IsNot DBNull.Value) Then
     Calender2.SelectedDate = Convert.ToDateTime(txt_dob.Text)
            End If
        Catch ex As Exception
        End Try
        Calender2.Visible = True
    End Sub

I am using the following code at calender selection changed event

txt_dob.Text = Calender2.SelectedDate.ToString()
 Calender2.Visible = False

I am getting the problem at Calender2.visible= true
as an exception like use new keyword at Calender2 but i have declared tht control earlier

any help is greatly appreciated.

thanks in advance.

Recommended Answers

All 12 Replies

As long as you have the calendar control in place you shouldn't need to declare it. Daft question, are you using the same spelling; reason for asking is that you have called it calender2, but the correct spelling would be calendar2. A common mistake is to allow Visual Web Developer (or whatever tool you are using) assign a name, and then you mis-spell it (I daren't count the hours I have spent looking for an error only to find its a typo!)

This code works (obviously don't know whether it is at all what you are wanting to do, but does demonstrate a working framework):

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub btn_dob_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_dob.Click
        Try
            If (txt_dob.Text.Trim() IsNot DBNull.Value) Then
                Calender2.SelectedDate = Convert.ToDateTime(txt_dob.Text)
            End If
        Catch ex As Exception
        End Try
        Calender2.Visible = True
    End Sub
    
    Protected Sub calendar_changed(ByVal sender As Object, ByVal e As System.EventArgs)
        txt_dob.Text = DateTime.Parse(Calender2.SelectedDate).ToString("dd/MM/yy")
        Calender2.Visible = False
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txt_dob" runat="server"></asp:TextBox>
        <asp:Button ID="btn_dob" runat="server" Text="Button" OnClick="btn_dob_click" />
        <asp:Calendar ID="Calender2" runat="server" OnSelectionChanged="calendar_changed"></asp:Calendar>
    </div>
    </form>
</body>
</html>

One other thing: I notice that you are using

Calender2.SelectedDate.ToString()

. This will also put the time in. Using

datetime.parse(Calendar2.selecteddate).ToString("dd/MM/yy")

would avoid this.

One final hint: you may have done this elsewhere, but you have not put an error trap for a value that isn't a valid date. Try :

IsDate(txt_dob.text)

Hi JD,

I am very thankful to you since u have resolved my doubt of how to omit the time from the captured value of date time picker..
Thanks alot once again...

Also i have a doubt regarding saving the form values into a database on a button click event but i was unable to do tht cud u please help me out with this query?

its very urgent bcos if iam unable to complete this by today i have a problem with my job...

So please help me out no ..

Thanks in advance...
Karthik..

At the top of your aspx web page you need to declare the data controls you are using and this depends on the database, to some extent, ie MS SQL, MySQL or Access.

As an example, to save to Access you would add:

<%@import Namespace="system.data" %>
<%@import Namespace="system.data.oledb" %>

Create the function to respond to the button click and declare variables. One way to create a data connection would be:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'set up variables
        'Open Data Connection
            Dim connection As OleDbConnection
            Dim command As OleDb.OleDbCommand
            Dim query As String
           connection = New OleDb.OleDbConnection("provider=microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("[I]insert database name[/I]"))
            connection.Open()
       ' create SQL query
       query = "INSERT INTO [I]table_name [/I]([I]field_name1, field_name2 etc[/I]) VALUES([I]value1[I], [I]value2 etc[/I] etc)"
command = New OleDbCommand(query, connection)
                Try
                    command.ExecuteNonQuery()
                Catch ex As Exception
                    errorchk2 = True
                    errorlabel.Text = ex.Message + " " + query
                End Try
            'Close Data Connection
            connection.Close()
            connection = Nothing

if you are using an SQL database the commands are slightly different but gives you an idea of the structure to get you started. A google search will reveal loads of sites that offer tutorials in data integration with asp.net.

However, if you are using an sql server, asp.net should be able to do most of the coding for you. I have found using the automated data functions in asp.net unreliable to access Access.

Protected Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
        myConnection = New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=HRDATA;Integrated Security=True")
        myConnection.Open()
        myCommand = New SqlCommand("Insert into Employee Data values(ddl_id.SelectedItem ,'" & txt_name.Text & "','" & txt_emerno.Text & "','" & txt_contnum.Text & "','" & txt_email.Text & "','" & txt_persemail.Text & "'),myConnection")

        'myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')","myConnection")
        ra = myCommand.ExecuteNonQuery()
        'MsgBox.Show("New Row Inserted" & ra)
        myConnection.Close()
    End Sub

Here is my code for submit button but i am getting the error as
ExecuteNonQuery: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


can anyone please help me i have also declared the variables tht i have used in this code
Thanks In advance..

Protected Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
        myConnection = New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=HRDATA;Integrated Security=True")
        myConnection.Open()
        myCommand = New SqlCommand("Insert into Employee Data values(ddl_id.SelectedItem ,'" & txt_name.Text & "','" & txt_emerno.Text & "','" & txt_contnum.Text & "','" & txt_email.Text & "','" & txt_persemail.Text & "'),myConnection")

        'myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')","myConnection")
        ra = myCommand.ExecuteNonQuery()
        'MsgBox.Show("New Row Inserted" & ra)
        myConnection.Close()
    End Sub

Here is my code for submit button but i am getting the error as
ExecuteNonQuery: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


can anyone please help me i have also declared the variables tht i have used in this code
Thanks In advance..

What's the name of the database - not entirely sure from your connection string

the name of the database is "HRDATA" and
the table i am using is "Employyedata"

Thanks for ur valuable time...

Have you declared your variables because can't see the declarations in the code you have given

I don't think strictly necessary I find very helpful for debugging by adding the fields you are inserting into in the query string (eg INSERT INTO employeedata (firstname,lastname) VALUES (value1, value2) )

Again for simplicity of debugging and to allow easy cutting and pasting of code, I prefer to create a separate string for the querystring rather than include the whole string in the command. I simply call it query. Eg query="INSERT INTO employeedata (firstname,lastname) VALUES (value1, value2)"

Most obvious problem to me though is the command itself and your use of "

You have written:

myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')", "myConnection" )

Note the double apostrophe around myconnection. Should be

myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')", myConnection )

Similarly, the other version would be:

myCommand = New SqlCommand("Insert into Employee Data values(ddl_id.SelectedItem ,'" & txt_name.Text & "','" & txt_emerno.Text & "','" & txt_contnum.Text & "','" & txt_email.Text & "','" & txt_persemail.Text & "')",myConnection)

HOpe that helps

Hi Jd,

I have a new requirement for displaying the calendar control as 3 comboboxes for date, month and year so that if the user selects the date then the complete date is to be captured like we see in lots of websites.
How can we do that any suggestion or any tutorial link is greatly appreciated...
Looking forward for your reply thanks in advance,
Karthik.

hi all,

My problem is solved for capturing the date value using 3 combo boxes May be this code snippet would be useful for anyone like me.....
in page_load paste this

If Not IsPostBack Then
            Dim i As Integer
            Dim values As New ArrayList
            For i = 1970 To Today.Year
                values.Add(i)
                ddlyear.DataSource = values
                ddlyear.DataBind()
                ddlyearj.DataSource = values
                ddlyearj.DataBind()
            Next
        End If
        d = (ddlday.SelectedItem.Value & ddlmonth.SelectedItem.Value & ddlyear.SelectedItem.Value).ToString()

        s = (ddldatej.SelectedItem.Value & ddlmonthj.SelectedItem.Value & ddlyearj.SelectedItem.Value).ToString()

and later u can use the string values any where in the code...
Regards,
Karthik.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.