Problem with asp.net calender control

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Problem with asp.net calender control

 
0
  #1
Dec 15th, 2008
Hi all,

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

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

I am using the following code at calender selection changed event
  1. txt_dob.Text = Calender2.SelectedDate.ToString()
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: jdharness is an unknown quantity at this point 
Solved Threads: 1
jdharness jdharness is offline Offline
Newbie Poster

Re: Problem with asp.net calender control

 
0
  #2
Dec 16th, 2008
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):

  1. <%@ Page Language="VB" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6. Protected Sub btn_dob_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_dob.Click
  7. Try
  8. If (txt_dob.Text.Trim() IsNot DBNull.Value) Then
  9. Calender2.SelectedDate = Convert.ToDateTime(txt_dob.Text)
  10. End If
  11. Catch ex As Exception
  12. End Try
  13. Calender2.Visible = True
  14. End Sub
  15.  
  16. Protected Sub calendar_changed(ByVal sender As Object, ByVal e As System.EventArgs)
  17. txt_dob.Text = DateTime.Parse(Calender2.SelectedDate).ToString("dd/MM/yy")
  18. Calender2.Visible = False
  19. End Sub
  20. </script>
  21.  
  22. <html xmlns="http://www.w3.org/1999/xhtml">
  23. <head runat="server">
  24. <title></title>
  25. </head>
  26. <body>
  27. <form id="form1" runat="server">
  28. <div>
  29. <asp:TextBox ID="txt_dob" runat="server"></asp:TextBox>
  30. <asp:Button ID="btn_dob" runat="server" Text="Button" OnClick="btn_dob_click" />
  31. <asp:Calendar ID="Calender2" runat="server" OnSelectionChanged="calendar_changed"></asp:Calendar>
  32. </div>
  33. </form>
  34. </body>
  35. </html>

One other thing: I notice that you are using
  1. Calender2.SelectedDate.ToString()
. This will also put the time in. Using
  1. 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 :

  1. IsDate(txt_dob.text)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Problem with asp.net calender control

 
0
  #3
Dec 16th, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: jdharness is an unknown quantity at this point 
Solved Threads: 1
jdharness jdharness is offline Offline
Newbie Poster

Re: Problem with asp.net calender control

 
0
  #4
Dec 16th, 2008
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:

  1. <%@import Namespace="system.data" %>
  2. <%@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("insert database name"))
            connection.Open()
       ' create SQL query
       query = "INSERT INTO table_name (field_name1, field_name2 etc) VALUES([I]value1[I], value2 etc 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Problem with asp.net calender control

 
0
  #5
Dec 17th, 2008
  1. Protected Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
  2. myConnection = New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=HRDATA;Integrated Security=True")
  3. myConnection.Open()
  4. 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")
  5.  
  6. 'myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')","myConnection")
  7. ra = myCommand.ExecuteNonQuery()
  8. 'MsgBox.Show("New Row Inserted" & ra)
  9. myConnection.Close()
  10. 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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Problem with asp.net calender control

 
0
  #6
Dec 17th, 2008
  1. Protected Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
  2. myConnection = New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=HRDATA;Integrated Security=True")
  3. myConnection.Open()
  4. 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")
  5.  
  6. 'myCommand = New SqlCommand("Insert into Employee values('"& txt_name.Text & "')","myConnection")
  7. ra = myCommand.ExecuteNonQuery()
  8. 'MsgBox.Show("New Row Inserted" & ra)
  9. myConnection.Close()
  10. 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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: jdharness is an unknown quantity at this point 
Solved Threads: 1
jdharness jdharness is offline Offline
Newbie Poster

Re: Problem with asp.net calender control

 
0
  #7
Dec 17th, 2008
What's the name of the database - not entirely sure from your connection string
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Problem with asp.net calender control

 
0
  #8
Dec 18th, 2008
the name of the database is "HRDATA" and
the table i am using is "Employyedata"

Thanks for ur valuable time...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: jdharness is an unknown quantity at this point 
Solved Threads: 1
jdharness jdharness is offline Offline
Newbie Poster

Re: Problem with asp.net calender control

 
0
  #9
Dec 19th, 2008
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:
  1. 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
Last edited by jdharness; Dec 19th, 2008 at 2:03 pm. Reason: Missing [ before COLOR
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kodingkarthik is an unknown quantity at this point 
Solved Threads: 0
kodingkarthik kodingkarthik is offline Offline
Light Poster

Re: Problem with asp.net calender control

 
0
  #10
Dec 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC