How to create an asp.net calendar link to database? - asp.net

We are trying to implement a calendar in an asp.net page and link it a database. The calendar should highlights dates based on data in the database tables similar to airline ticket booking calendar.
Thank you

No one is going to write an application for you here... If you have a specific question you should ask it. Without that here is my best advice.
Start by looking at how to implement asp.net's calendar control:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar%28v=vs.80%29.aspx
Then look at using ADO.NET to communicate from your web app to your db. Here is a good place to start:
http://msdn.microsoft.com/en-us/library/e80y5yhx.aspx

A calendar has a property SelectedDates which is a list of the dates that have been selected. All you need to do if you get a list of dates from a database is add them to the property SelectedDates.
For example:
Public Sub Calendar_Prerender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar.PreRender
Dim tomorrow As DateTime = Now.AddDays(1)
Dim nextday As DateTime = Now.AddDays(2)
Calendar.SelectedDates.Add(tomorrow)
Calendar.SelectedDates.Add(nextday)
End Sub
The calendar loads with tomorrow and the day after selected.

Related

how to use session in transferring id from one aspx page to another aspx page

I have two aspx pages. The first one contains the gridview list of all the vessels and the other one consists the application form for permit. Once i select a row in the gridview list,
a button for the permit application will be visible, this button once clicked, redirects to the second page which is the application form for permit. The problem is i have to get the id of the vessel from the selected row on the gridview list (first aspx page) and transfer it to the second aspx page. The id should be inserted in the permit application table together with the other data in the permit application form.
It sounds like this is your situation, roughly speaking: you have aspx pages A and B. Your user inputs something on A that is needed by B to render something else. To me the fact that you are rendering a table on B is irrelevant; if you could simply echo the value inputted from A onto B you would know what to do from there. So I am going to ignore the data table stuff.
You could use a session variable to transmit this value from A to B, but you probably shouldn't. There are a few good reasons not to do so; for starters, session variables are persisted in memory and therefore need to be managed.
If you insisted on using session variables, you would handle the event of, say, clicking submit on A by storing the text box's value into a session variable of your choice:
Public Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Session("my_session_variable_name") = myTextBox.Text
End Sub
And then in B, in the appropriate lifecycle event (say, on load) you would read the session variable and produce the table or whatever you are trying to accomplish:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("my_session_variable_name") <> "" Then
id = Session("my_session_variable_name")
End If
But I would highly discourage you from doing it this way. Instead, try to make your B page take in either a query string parameter or a POSTed body member, and read it using Request.QueryString[] or Request.Form[].
You should use CommandName and CommandArgument attributes of the Button.
<asp:Button runat="server" ID="btn" CommandName="Permit" CommandArgument="6" />
But CommandArgument must be bind OnRowDataBound event of the gridview. So when you click you will check command name and get command argument value and pass it to permit form.

Date and Time picker in ASP.net

I know this question is on SO a few times already but I cannot find a working or suitable answer.
I want a date picker which includes a time picker. I don't want any fancy frills etc, just something that works. Preferably one control I can drag into the tool box in VS, register at the top and work with. I have been playing around with many versions the last few days and I can't get one to work. They normally seem to include too many files for me or something stupid. A project I am working on now hinges on this stupid thing so any advice would be welcome. I have previously managed to get a calendar control and put some validation on a text-box to pick times. However I cannot take both of these values and enter them into my database as on Date-time field because when I try to use "Selected-date" to get the time it naturally says that text-boxes cannot be members of the "Selected-date" property.
As I have a calendar control in to pick the date and a validated textbox to type in the time I tried to use the following code.
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim time As String() = txtTime.SelectedDate.ToString().Trim().Split(" "C)
Dim [date] As String() = cldDate.SelectedDate.ToString().Trim().Split(" "C)
Dim datetime__1 As String = [date](0) & " " & time(1) & time(2)
Dim DateTimeValue As DateTime = DateTime.Parse(datetime__1)
Response.Write(DateTimeValue.ToString())
' Update database with DateTimeValue.
End Sub
The code txtTime.SelectedDate.ToString() is a problem here. It says that SelectedDate is not a supported member of textbox. This is code that I have came across. I'm pretty new to coding.
You could create a new instance of the DateTime structure. The DateTimePicker.SelectedValue returns a Nullable(Of DateTime), so you can use the values of this to create a new DateTime (along with the values from your textboxes).
For example:
Dim myDateTime As New DateTime(cldDate.SelectedDate.Year, cldDate.SelectedDate.Month, cldDate.SelectedDate.Day, HourValue, MinuteValue, 0)
Just replace HourValue and MinuteValue with your values from your textboxes. Note you should convert these to Integers.
Note the 0 at the end represents seconds.
This should save to the database fine.
For more info on DateTime see here.
if jQuery is an option i would look at this plug in
clean and easy to implement
You don't mention whether a commercial product would be an option for you.
We are using telerik's RadDateTimePicker in our projects (and there are certainly other commercial products). See this page for a demo of the datetime picker control.

PagedDataSource not working with previous/next pages

I'm having a hard time with this scenario and hoping someone can help me clarify where I'm missing a step/logic. I tried searching online but I couldn't find an example that addressed this issue.
We are setting up a search page that when first loads shows a bunch of options (e.g., textboxes, checkboxes, etc). The user will fill out the form and submit the form back to itself. Once posted back, the page will build and run a SQL query (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3) against the database with the user's options and then the results will show up. This part works ok.
The part that breaks down is when I am trying to add pagination. The result set is a DataTable bound to a Repeater. I am using a PagedDataSource to add pagination. Pagination works great for the first page but for subsequent pages, it does not work. Basically, instead of returning the next page of the result requested (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3), the results returned is the SQL query before appending the user's search options (e.g., SELECT ID FROM Customers).
I think my basic problem is that I'm not sure how to distinguish a normal Page.IsPostBack and paginating through results. The reason why this is causing problems is because I don't want to be regathering form data, rebuilding query, and requerying the DB.
The examples that I found online involved pagination of a DataTable that is rebuilt every time the page loads (e.g., Not Page.IsPostBack).
Here is a rough outline of our code:
Public dtCustomers As DataTable = New DataTable()
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
' When the page first loads, show the search form with search options.
If Not Page.IsPostBack Then
ShowSearchForm()
' Once the form is submitted, show the search results.
Else
' ----------------------------------------
' This is the part that I'm having trouble with. How do I skip the following two steps (GatherFormData() and BuildDT()) when the user requests a subsequent page?
GatherFormData()
dtCustomers = BuildDT()
' ----------------------------------------
BindData()
End If
End Sub
Sub BindData()
Dim objPDS As PagedDataSource = New PagedDataSource()
objPDS.DataSource = dtCustomers.DefaultView
objPDS.AllowPaging = True
objPDS.PageSize = intNumPerPage
objPDS.CurrentPageIndex = Me.ViewState("_CurrentPage")
rptSearchResults.DataSource = objPDS
End Sub
' Subroutine called when the previous page button is pressed.
Sub GoToPrevPage()
' Set viewstate variable to the previous page.
Me.ViewState("_CurrentPage") -= 1
BindData()
End Sub
' Subroutine called when the next page button is pressed.
Sub GoToNextPage()
' Set viewstate variable to the next page.
Me.ViewState("_CurrentPage") += 1
BindData()
End Sub
Note: I understand that the DataTable will have to be put into cache or a session variable, but haven't decided the best method.
Please excuse the code outline but the actual code is massive so the simplification is to make it easier to get to the heart of the issue.
Let me know if any of that was unclear. Thanks in advance!
I am assuming that you are going to store your data in session/cache (I would prefer the later but there can be use case for storing it in session) - you can store the key in the view-state and presence of key could be use to determine if post-back is for pagination or not. For example,
if (ViewState["dataKey"] == null)
{
// first form submittal, do the search
GatherFormData();
dtCustomers = BuildDT();
// store it in cache
ViewState["dataKey"] = Guid.NewGuid().ToString(); // create a key
Cache.[ViewState["dataKey"].ToString()] = dtCustomers; // TODO use Add method to control expiration etc
}
Its important that you clear the view-state when the search is reset (or similar condition)
Lastly, it is also possible to do the paging from database/data-store (for example, using ranking functions in SQL Server) so that you need not have to store the search results into the session/cache but rather do a database trip at each post-back. Such approach is useful when the complete result set size can be huge.

PageIndexChanging Without Binding

I have datagridview linked with dataSource, this data source get data from table has 9 Million record and I'm searching in this table by name, after Binding data when need to change PageIndex this requires to research in 9 million records by this code:
Any method to change PageIndex without binding it again?
Protected Sub GVCenteralSearch_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GVCenteralSearch.PageIndexChanging
GVCenteralSearch.PageIndex = e.NewPageIndex
GVCenteralSearch.DataSource = CentralSearch_DS
GVCenteralSearch.DataBind()
End Sub
Notes : This Is Based on WebApp (ASP.net)
Not that I know of. You do have a few options though, you can cache the data so you don't have to get it from the database again. You could also look into javascript alternative to paging the data (jQuery DataTable plugin). Alternatively you can re-write the query to only grab the records you need. This would still take the round trip to get the data displayed, but it won't be as significant.

gridview filtering via joined tables

I'm trying to display data which comes from a join on two tables (or more) in a gridview
I want to be able to filter the result set via user given input (text boxes on page)
I have tried the standard tutorials but cannot find something which goes beyond displaying one table result sets in the gridview. If I go through the configure gridview and configure datasource wizard for any datasource (sqlDatasource, object, entitydatasource), when I use multiple tables I cannot use the 'where' parameters in the wizard, and therefore need to have the selecting code in the code-behind, but i'm unsure where exactly to put this.
e.g. if I have
Protected Sub button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles button1.Click
sqldatasource1.selectcommand = "select * from yourtable where modelfamily
like '%" & textbox1.text & "%' or description like '%" &
textbox1.text & "%'"
sqldatasource1.databind()
End Sub
then this code will help with a search button for part 2 of my question, e.g. if button1 is a search button then I can incorporate the text box values in the query and bind,
but where would I place the intiial data binding code, in page_load? And where shall I place the other other code? Please could you provide me a link to a solution, or better help me here please.
I want to be able to:
use entity data source to do this (how can I do this multiple join in entity framework, or other source if much easier)
I want to display all the results intially (coming from two tables) with paging and sorting enabled
if text box has a value and search button clicked, then subset of the data based on the value is shown, and is also page-able/sort-able within itself.
Please can you show me examples of where would the code sit, which events and what will it look like?
Have a look at this Inner Join statement and gridview

Resources