So I'm trying to create a calendar tool that pulls in dates from an SQL Database.
I've created a TableAdapter which is here: http://bit.ly/KRaTvr
This is the small bit of ASP I have to create my calendar:
<asp:Calendar ID="calEvents" runat="server"> </asp:Calendar>
Then I have the C# in my project used to pull the information through.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BreakyBottomTableAdapters;
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Data Table
BreakyBottom table;
//Table Adapter
BreakyBottomTableAdapters.ScheduleDate ta;
ta = new BreakyBottomTableAdapters.ScheduleDate();
//Populate the table using the Table Adapter passing current date
table = ta.GetData(e.Day.Date);
//Check the number of rows in the data table
if (table.Rows.Count > 0)
{
//Change the background colour to gray
e.Cell.BackColor = System.Drawing.Color.Gray;
//Allow the Day to be selected
e.Day.IsSelectable = true;
}
}
}
Now I know I'm missing something but I can't for the life of my figure out what it is.
Here is a screenshot of the compile error I get: http://bit.ly/ISuVsT - I know I'm probably missing something insanely obvious but any assistance would be appreciated.
Best Regards
The compiler is telling you that EventArgs does not have any member called Day which you are apparently using in your code incorrectly:
protected void Page_Load(object sender, EventArgs e)
{
....
table = ta.GetData(e.Day.Date); //WRONG: Day is not a member of EventArgs
}
If the idea is to use the Current date, as you mentioned, then do this:
table = ta.GetData(DateTime.Now);
Related
This is my first question here at Stack Overflow and i will be as precise as possible.
I am working on an ASP.NET Web application and i have a gridview which have a XML as a datasource. I already have it bound as a datasource and it works perfect.
I have a textbox and a button in my project and want to search in the gridview.
Here is an example of one of the nodes in the XML file.
https://i.imgur.com/weTlrh1.png
The problem I currently have is that I am stuck at the search gridview part.
I have as I said before a textbox and a button and when i write a value in the textbox and press on the button i want to search the gridview and its nodes to check if for an example any titles contains the words from the textbox.
Right now I have the corresponding code:
https://i.imgur.com/l2lz8JL.png
This code is in the buttonSearch Click event. The SearchBookBox.text is the Text i want to search for in the Gridview.
I am stuck right now and i need some help.
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("/NewFolder1/books.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
The above code is the code which i bind the gridview to the corresponding xml file. This code is in my Page_Load Event.
I have tried different ideas to solve this issue, but nothing works.
Right now the code is like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace BookSearch
{
public partial class BookSearchForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ErrorLabel.Visible = false;
AddBooksXML();
}
private void AddBooksXML()
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("/NewFolder1/books.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void BookSearchButton_Click(object sender, EventArgs e)
{
if(SearchBookBox.Text != null)
{
foreach (XmlNode node in ds.GetXml)
{
}
}
else
{
Response.Write("ERROR!");
}
}
}
}
I really need some help. As i said, i have tried different ideas, but I am stuck and I can't get anywhere.
I think this is a possible solution.
protected void XmlSearch(string searchText)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("/NewFolder1/books.xml"));
var dat = from books in ds.Tables[0].AsEnumerable()
where books.Field<string>("author") == searchText || book.Field<string>("title") == searchText
select books;
GridView1.DataSource = dat.CopyToDataTable();
GridView1.DataBind();
}
This will search in author and title for exact matches its probably not the best solution but its a example of how you could do it.
Requires
using System.Linq;
EDIT
If you wanna loop through them you can use
foreach (var book in dat)
{
var bookInfo = String.Format("{0} {1}", book["author"].ToString(), book["title"].ToString());
}
I am new to asp.net and trying to do simple things first. Now i am trying to make a simple library database. In my scenario user enters the title that he needs to search and then clicks search button. Here is the screenshot of my simple user interface:
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Pages_Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bSearchButton_Click(object sender, EventArgs e)
{
string searchedItem = tSearchBox.Text;
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Users\\SUUSER\\Documents\\Visual Studio 2010\\Projects\\Library\\LibWebSite\\App_Data\\LibDatabase.mdf;Integrated Security=True;User Instance=True";
Int32 verify;
string query1 = "";
if (SearchBy.SelectedValue == "Search by title")
{
query1 = "Select count(*) from Items where Title='" + tSearchBox.Text + "'";
}
}
}
My question is, my query finds the number of items with that title, but does not print each item to the screen. How can i print out the search results to the screen? I mean how can i show the search results in another web page? I appreciate any helps.
Thanks
Tutorials such as this should help:
http://www.codeproject.com/Articles/8659/Mastering-ASP-NET-DataBinding
Change your query to:
"Select Title,count(1) from Items where Title='" + tSearchBox.Text + "'";
I am trying PetaPoco with my Asp.Net Project. How can I use PetaPoco with Asp.Net GridView. I am new at web programming.
I tried all code samples in the blog. They all are working with console app. But in Asp.Net I could not bind the datasource to GridView.
Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PetaPoco;
using northwind;
using System.Dynamic;
namespace Mng
{
public partial class Default : System.Web.UI.Page
{
public northwindDB db;
protected void Page_Load(object sender, EventArgs e)
{
db = new northwindDB();
if (!Page.IsPostBack)
{
var q = db.Query<Product>("SELECT top 10 * FROM Products");
grdMng.DataSource = q;
}
else
{
Response.Write("Postback occurs");
}
}
}
}
Call DataBind method to build the grid
grdMng.DataSource = q;
grdMng.DataBind();
Another .net newbie question
I created a class that inherits System.Web.UI.Page with the intention of overriding the preload and Unload functions. But Visual Studios complains I can't override because "System.Web.UI.Page.PreLoad" is not a function. What's wrong with my code? Here it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class BTPage : System.Web.UI.Page
{
protected SqlConnection cnx;
public override void PreLoad(object sender, EventArgs e)
{
cnx = new SqlConnection(ConfigurationManager.AppSettings["btdatabase"]);
cnx.Open();
}
protected void Unload(object sender, EventArgs e)
{
cnx.Close();
}
}
If there's anything else alarming about my code, please tell me. I'm new to .Net and I'm not sure if I'm doing things in the ".NET way".
What I also want to do is have every web page inherit from BTPage so that they all open a connection to the database on preload.
PreLoad is not a method of the Page type. You need to override OnPreLoad
I want to get the selected Indices or Items only in checkBox lsit instead of iterating through each item as Like is there in ListBox.
I am Getting Selected Value In tWo case In this way:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChkBxList_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string li = "";
foreach(ListItem lt in CheckBoxList1.Items)
{
if(lt.Selected)
li += lt.Text;
}
Response.Write(li);
}
protected void Button2_Click(object sender, EventArgs e)
{
string li = "";
foreach (int lt in ListBox1.GetSelectedIndices())
{
li += ListBox1.Items[lt].Text;
}
Response.Write(li);
}
}
In ListBox we have the Option To get Seected Only Items is there any For Check Box List?
I dont think there is but you could use this extension method that does exactly that:
public List<ListItem> GetSelectedItems(this CheckBoxList checkBoxList)
{
List<ListItem> list = new List<ListItem>();
foreach(ListItem lt in checkBoxList)
{
if(lt.Selected)
list.Add(lt);
}
return list;
}
//Call it like this
checkBoxList.GetSelectedItems();
You actually answered your own question. There is no way to get the selected only items in the CheckBoxList control, unlike the ListBox control.
This article has an explanation and a work around help method.
http://weblogs.asp.net/jgalloway/archive/2005/10/02/426346.aspx