Jquery datepicker with entity framework - asp.net

<form id="Form1" runat="server">
**<input type="text" id="DateInput" />**
<asp:Repeater ID="DataViewer" runat="server">
<ItemTemplate>
<div style='border: 1px; width: 600px; overflow-x: auto; overflow-y: hidden;'>
<div style='float: left;'>
<%# Eval("DriverName") %> </div>
</div>
</ItemTemplate>
</asp:Repeater>
this is my function:
protected void Page_Load(object sender, EventArgs e)
{
OrderDataRepository rep = new OrderDataRepository();
var results = rep.GetAllOrderData().Where(x => x.POD_DATE == ????????????????).
GroupBy(o => o.User).
Select(g =>
new
{
DriverId = g.Key.Id,
DriverName = g.Key.Name,
OrderCount = g.Count(),
OrderCountWhereNameIsNotNull =
g.Count(o => o.RECEIVE_NAME != null)
}).ToList();
DataViewer.DataSource = results;
DataViewer.DataBind();
}
at the moment i get all the results from the table,
i want to add Datepicker for jQuery http://keith-wood.name/datepick.html
<script src="Scripts/jquery.js" type="text/javascript"></script>
when user will pick a day it needs to load all the results for the day that the user picked
please show me how it should be done using jquery with entity framework

You'll need a function in your repository some thing like:
Function SelectOrdersByDate(date as string) as ienumerable(of Order)
Using yourcontext as new context
Dim query = yourcontext.Orders.Where(function(o) o.OrderDate = date).ToList
return query
End Using
End Function
Then call this function via ajax/jquery

Related

Displaying multiple entries on a database into an aspx page

I am trying to display multiple lines from a database into an aspx page. My aspx page name is news.aspx and my table is called news.
I have configured my aspx with the code to display what I want my page to look like. I have attached an image to show I would like it to look like when the page is loaded. The aspx page uses ItemTemplate & asp:Repeater to display the multiple lines.
My aspx page is configured as follows:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-lg-12">
<div class="form-group alert alert-info">
<asp:label runat="server" ID="lblNewsEdits">Latest Sports & Social News Items</asp:label>
</div>
</div>
</div>
</div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-lg-12">
<p>
Title:
<asp:Literal ID="litTitle" runat="server"></asp:Literal>
(<asp:Literal ID="litDatePosted" runat="server"></asp:Literal>)
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-lg-12">
<p>
Title:
<asp:Literal ID="litNewsContent" runat="server"></asp:Literal>
</p>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
My aspx.cs page is configured to pull the information from the news table, I want three column of data to be display in the aspx page (Title, DatePosted and NewsContent).
For the three lines below, I am getting error: "The name "..." does not exist in the current context.
litTitle.text = reader ["Title"].ToString();
litDatePosted.Text = reader["Date Posted"].ToString();
litNewsContent.Text = reader["News Content"].ToString();
The aspx.cx page is configured as follows:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string getNewsQuery = "SELECT Id, Title, DataPosted, Newsontent FROM News WHERE Id = #id";
//get email based on id
SqlCommand getNewsCommand = new SqlCommand(getNewsQuery, connection);
object id = null;
getNewsCommand.Parameters.AddWithValue("#id", id);
connection.Open();
SqlDataReader reader = getNewsCommand.ExecuteReader();
while (reader.Read())
{
litTitle.txt = reader ["Title"].ToString();
litDatePosted.Text = reader["Date Posted"].ToString();
litNewsContent.Text = reader["News Content"].ToString();
}
reader.Close();
connection.Close();
}
My database table is configured as follows:
CREATE TABLE [dbo].[News] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[DatePosted] DATE NOT NULL,
[NewsContent] NTEXT NOT NULL,
[IsRead] BIT DEFAULT ((0)) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)[![enter image description here][1]][1]
);
Any ideas?
Try to format your code like this:
litTitle.txt = reader["Title"].ToString();
litDatePosted.Text = reader["DatePosted"].ToString();
litNewsContent.Text = reader["NewsContent"].ToString();
Also in your sql query you have a typo in the SELECT statement, you typed
'Newsontent' instead of 'NewsContent'
EDIT:
Try using the grid view instead of table. GridView should format it selfe acording to the sorce provided by the SqlDataAdapter
protected void Pageaaa_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
string getNewsQuery = "SELECT Id, Title, DataPosted, Newsontent FROM News WHERE Id = #id";
//get email based on id
SqlCommand getNewsCommand = new SqlCommand(getNewsQuery, connection);
object id = null;
getNewsCommand.Parameters.AddWithValue("#id", id);
connection.Open();
SqlDataReader reader = getNewsCommand.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dataTable = new DataTable();
da.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
connection.Close();
da.Dispose();
}

Bootstrap grid not aligned properly when dynamically creating div

I am trying to retrieve product information such as name,imagepath etc from database using repeater and display in the dataview using code behind. I could successfully retrieve and display the detail but the alignment of the div is not proper.
aspx code:
<div class="row">
<asp:Repeater ID="Repeater2" runat="server"></asp:Repeater>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img width="250px" height="300px" src="images/<%#Eval("ImagePath") %>" alt="<%#Eval("ProductName") %>">
<div class="caption">
<h4><%#Eval("ProductName") %></h4>
<h4><%#Eval("UnitPrice")%></h4>
</div>
<div class="clearfix">
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
code behind:
SqlDataAdapter sda = new SqlDataAdapter("SELECT ProductName, UnitPrice, ImagePath FROM Products", con);
DataTable dt = new DataTable();
sda.Fill(dt);
DataView dv = new DataView(dt);
dv.Sort = "ProductName"; // Sort by product name
Repeater1.DataSource = dv;
Repeater1.DataBind();
current output:
screenshot
Expected output:
The fourth image should come below the first image according to bootstrap gridview concept which is not happening in my case and because of which the entire layout changes.
I am a beginner and have just started to learn bootstrap.
There are two ways to resolve this issue:
1) End the row after 3 columns and start 2nd row for next 3 columns.
2) Add equal height for columns. You can use following script and add 'resize-height' class to each column.
function setHeight(column) {
var maxHeight = 0;
//Get all the element with class = col
column = jQuery(column);
column.css('height', 'auto');
//Loop all the column
column.each(function () {
//Store the highest value
if (jQuery(this).height() > maxHeight) {
maxHeight = jQuery(this).height();
}
});
//Set the height
column.height(maxHeight);
}
jQuery(window).on('load resize', function () {
setHeight('.resize-height');
});

Custom Panel didn't render well

I'm trying to customize the rendering of an ASP.NET Panel in this way...
[DefaultProperty("ID")]
[ToolboxData("<{0}:NFormPanel runat=server></{0}:NFormPanel>")]
[Description("Aldammam panel.")]
[ParseChildren(false)]
[PersistChildren(true)]
public class NFormPanel : Panel, INamingContainer
{
protected override void RenderContents(HtmlTextWriter output)
{
var sb = new StringBuilder();
sb.AppendLine("<table class=\"con-table\" style=\"width: 100%;\">");
output.Write(sb.ToString());
if (HasControls())
{
this.RenderChildren(output);
}
sb.Clear();
sb.AppendLine("</table>");
output.Write(sb.ToString());
}
}
...but the output renders the panel controls first then renders the table, like this...
<div>
<input name="TextBox1" type="text" id="TextBox1">
<table class="con-table" style="width: 100%;">
</table>
</div>
I would like the child controls inside the table as in the example below...
<div>
<table class="con-table" style="width: 100%;">
<input name="TextBox1" type="text" id="TextBox1">
</table>
</div>
Use the render method instead:
protected override void Render(HtmlTextWriter output)
{
var sb = new StringBuilder();
sb.AppendLine("<table class=\"con-table\" style=\"width: 100%;\">");
output.Write(sb.ToString());
if (HasControls())
{
//Alternatively call base.Render() which renders the panel's DIV
this.RenderChildren(output); //renders inner children only without panel DIV
}
sb.Clear();
sb.AppendLine("</table>");
output.Write(sb.ToString());
}

how to avoid inline code in .aspx .ascx

I have a usercontrol (.ascx) like this:
<% if (HasAccessMediaPlans) { iPortletCounter++; %>
<div class="orange-portlet-box">
<div class="HomeModulePortletTitle">Plans</div>
<p>Create // Edit // Review</p>
</div>
<div style="clear: both;"></div>
<% } %>
where HasAccessMediaPlans is a variable defined in user control's code behind(.ascx.cs) and it's assigned in page load.
protected Boolean HasAccessMediaPlans = false;
protected void Page_Load(object sender, EventArgs e) {
HasAccessMediaPlans = SessionState.CurrentUser.HasModuleAccess(MediaString + " Plans");
}
My question is: how can I avoid inline server code embeded in <% %> at my usercontrol markup(.ascx) ?
You can wrap this piece of code with a server side container control (say <div id="wrapper" runat="server">) and on the server side assign its visibility properties in the wanted manner.
This avoids littering your .aspx/.ascx files with code and keeps code in the code-behind file.
<div class="orange-portlet-box" id="dvBox" runat="server">
<div class="HomeModulePortletTitle">
<a id="aLink">Plans</a>
</div>
<p>Create // Edit // Review</p>
</div>
<div style="clear: both;"></div>
In code behind
Boolean HasAccessMediaPlans = false;
protected void Page_Load(object sender, EventArgs e)
{
HasAccessMediaPlans = SessionState.CurrentUser.HasModuleAccess(MediaString + "Plans");
dvBox.Visible = HasAccessMediaPlans;
aLink.HREF = RootPath + "MediaPlanning/Default.aspx";
}

ASP.NET Decimal.Parse()

I have problem in webpage , Visual Studio claims that problem is with this line decimal newAmount = PLNamount * Decimal.Parse(item.Value);. The solution is crushed when I choose the Current (web page is simple current converter) .
this is listing of CurrencyConverter.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
// The HtmlSelect control accepts text or ListItem objects.
Currency.Items.Add(new ListItem("Euros", "0.25"));
Currency.Items.Add(new ListItem("US Dollar", "0.32"));
Currency.Items.Add(new ListItem("British Pound", "0.205"));
}
}
protected void Convert_ServerClick(object sender, EventArgs e)
{
decimal PLNamount;
bool success = Decimal.TryParse(PLN.Value,out PLNamount);
if (success)
{
PLNamount = Decimal.Parse(PLN.Value);
ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = PLNamount * Decimal.Parse(item.Value); //prollematic line
Result.InnerText = PLNamount.ToString() + " Polish PLN = ";
Result.InnerText += newAmount.ToString() + " " + item.Text;
}
else Result.InnerText = "Invalid content";
}
}
This is listing of CurrencyConverter.aspx
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Currency Converter</title>
</head>
<body>
Simple Currency Converter in ASP.NET web forms <br />
<form id="Form1" runat="server">
<div>
Convert:
<input type="text" ID="PLN" runat="server" />
Polish PLN to Euros.
<select ID="Currency" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server"
OnServerClick="Convert_ServerClick" />
<br /><br />
<div style="font-weight: bold" ID="Result" runat="server"></div>
</div>
</form>
</body>
</html>
This Code is changed listing from apress "Pro ASP.NET 3.5 in C# 2008, Second Edition" (original version) from chapter 5. Link to source code apress.com/book/downloadfile/3803 . I have exactly the same problem in the same line
Original source code from the book
aspx
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="CurrencyConverter.aspx.cs" Inherits="CurrencyConverter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Currency Converter</title>
</head>
<body>
<form ID="Form1" method="post" runat="server">
<div style="border-right: thin ridge; padding-right: 20px; border-top: thin ridge;
padding-left: 20px; padding-bottom: 20px; border-left: thin ridge; width: 531px;
padding-top: 20px; border-bottom: thin ridge; font-family: Verdana; background-color: #FFFFE8">
Convert:
<input type="text" ID="US" runat="server" style="width: 102px" /> U.S. dollars to
<select ID="Currency" runat="server" />
<br /><br />
<input type="submit" value="OK" ID="Convert" runat="server" OnServerClick="Convert_ServerClick" />
<input type="submit" value="Show Graph" ID="ShowGraph" runat="server" OnServerClick="ShowGraph_ServerClick" />
<br /><br />
<img ID="Graph" alt="Currency Graph" scr="" runat="server" />
<br /><br />
<div style="font-weight: bold" ID="Result" runat="server"></div>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
// The HtmlSelect control accepts text or ListItem objects.
Currency.Items.Add(new ListItem("Euros", "0.85"));
Currency.Items.Add(new ListItem("Japanese Yen", "110.33"));
Currency.Items.Add(new ListItem("Canadian Dollars", "1.2"));
}
Graph.Visible = false;
}
protected void Convert_ServerClick(object sender, EventArgs e)
{
decimal amount;
bool success = Decimal.TryParse(US.Value, out amount);
if (success)
{
// Retrieve the selected ListItem object by its index number.
ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = amount * Decimal.Parse(item.Value);
Result.InnerText = amount.ToString() + " U.S. dollars = ";
Result.InnerText += newAmount.ToString() + " " + item.Text;
}
else
{
Result.InnerText = "The number you typed in was not in the correct format. ";
Result.InnerText += "Use only numbers.";
}
}
protected void ShowGraph_ServerClick(object sender, EventArgs e)
{
Graph.Src = "Pic" + Currency.SelectedIndex.ToString() + ".png";
Graph.Visible = true;
}
}
Problem solved
This is link to print screen with this error. http://img210.imageshack.us/f/currencyerror.png/
I think the answer is localisation based. I suspect you are parsing "0.25" as a decimal in your a culture style which uses commas as decimal separators. What you need to do is to specify what style the numbers are in so that it interprets them correctly. One way to do this is:
decimal newAmount = PLNamount * Decimal.Parse(item.Value, CultureInfo.InvariantCulture.NumberFormat)
Hopefully people will comment if there is a nicer way of doing it.
Hope that helps. I've not confirmed that this is definitely your problem but if I try to parse the number as pl-PL (just guessing you are polish from the page) then it fails for me on that line you quoted so I reckon I'm on the right lines. :)
Edit to add: The other option is to change your dropdown to have values like "0,25" which should parse correctly. However, I personally would prefer to use the CultureInfo option.
Interestingly when I enter numbers into the box on the page it seems to recognise "0.9" and "0,9" as the same thing. Not sure why it works there but I guess its related to tryParse and Parse workign subtly differently.
You don't need to do both:
bool success = Decimal.TryParse(PLN.Value,out PLNamount);
if (success)
{
PLNamount = Decimal.Parse(PLN.Value);
If the TryParse succeeded then PLNamount will contain the correct value. Check the MSDN documentation for TryParse.
Also:
ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = PLNamount * Decimal.Parse(item.Value); //prollematic line
if the value in item isn't a string representing a number Parse will throw an exception. For safety you should use TryParse on this value as well (if it's not already a numeric type of course).
you should delete the following line
PLNamount = Decimal.Parse(PLN.Value);
if(success) ... then PLNamount are already parsed, it is an out parameter...
Parse throws an exception if the value being parsed isn't a decimal. TryParse is safer in that respect; it returns a bool if it is a decimal, and returns the value through an output parameter, but you should check that TryParse returns true.
if (decimal.TryParse("12", out decValue)) { .. }
Using TryParse for the item check, where the error occurs, too may help...
The problem is't solved .
item.Value is string in 100% //debuger confirms that.
I don't know what I have to do.
the answer is this, assuming it's a value from a DropDown that always contains percentages.
decimal ItemValue = Decimal.Parse(Item.Value.Substring(0, Item.Value.IndexOf('%')));
It strips the % from the string and then converts the remainder to a decimal.
If you look at your debug, item.value = "US Dollar". This is a string value, you've probably want the "Text" property.

Resources