Search in a xml gridview. - asp.net

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());
}

Related

Absolute expiration and sliding expiration are not working(caching)

// guys please help the absolute caching and sliding caching are not working i am unable to find where is the problem
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 System.Web.Configuration;
using System.Web.Caching;
namespace WebApplication154
{
public partial class WebForm1 : System.Web.UI.Page
{
string cs =
WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
code to bind grid view when the page is loading
protected void Page_Load(object sender, EventArgs e)
{
lblserver.Text = DateTime.Now.ToString();
DataSet ds = getdata();
Cache["products"] = ds;
grid1.DataSource = ds;
grid1.DataBind();
}
i am passing data from database to dataset
public DataSet getdata()
{
SqlConnection conn = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spfrag", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
the data is not caching even i mentioned the below code
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Cache["products"] != null)
{
DataSet ds = (DataSet)Cache["products"];
grid1.DataSource = ds;
grid1.DataBind();
}
else
{
DataSet ds = getdata();
Cache.Add("products", ds, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromSeconds(20), CacheItemPriority.Default, null);
grid1.DataSource = ds;
grid1.DataBind();
}
}
}
You probably want to try pulling from the cache in getdata() and only pull from the database if the cache is empty there. Check out the Cache Repository Pattern for an example
The way this coded now you would only ever pull from the cache on the second btnsubmit_Click event fire. You would end up going to the database on every Page_Load too, which fires on btnsubmit_Click too.

how to show a record values in text box in asp.net

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.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
void binddata()
{
string pid = Request.QueryString["Prod_Id"];
con = new SqlConnection("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
da = new SqlDataAdapter("Select * from Products where Prod_Id='" + pid + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
listview.DataSource = ds;
listview.DataBind();
}
}
}
the user is directed to the record updation page when he click the edit link in the record list page. what should i do write in datasource and databind
You have to extract the values from DataSet like
string name = ds.Tables[0].Rows[0]["name"].ToString();
Here i am assuming that you have a name as a field in your select query, you have to use your fields.
After getting that field assign it to TextBox like
TextBox1.Text = name ;
After that run your update query based on Primary Key.
One Important Suggestion :-
Try to use Page.IsPostBack property of your Page on Page_Load.Need to change your Page_Load like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
Hope you understand and works for you.
Try to use this.
TextBox1.Text = ds.Tables[0].Rows[0]["name"].Tostring();

Printing to screen after searching in the database in asp.net

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 + "'";

C# Calendar Tool ASP.NET

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);

Dynamically adding Devexpress GridControl to C# windows application

I want to add Devexpress GridControl dynamically. At runtime I want to show the Filter Row. Also I want to have a button on the same form that has the dynamically created GridControl. When the button is clicked it should show the Filter Dialog popup for the grid control.
The provided sample does what you ask for.
Create a Form called Form1.
Create a Button called button1 and Dock it to
the top of the form.
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
namespace Samples
{
public partial class Form1 : Form
{
private GridControl grid;
private GridView view;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
view.ShowFilterPopup(view.Columns[0]);
}
private void Form1_Load(object sender, EventArgs e)
{
grid = new GridControl();
view = new GridView();
grid.Dock = DockStyle.Fill;
grid.ViewCollection.Add(view);
grid.MainView = view;
view.GridControl = grid;
view.OptionsView.ShowAutoFilterRow = true;
GridColumn column = view.Columns.Add();
column.Caption = "Name";
column.FieldName = "Name";
column.Visible = true;
// The grid control requires at least one row
// otherwise the FilterPopup dialog will not show
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Rows.Add("Hello");
table.Rows.Add("World");
grid.DataSource = table;
this.Controls.Add(grid);
grid.BringToFront();
}
}
}

Resources