Search GridView - asp.net

I have a guestbook at the moment displaying "comment", "comment_time", "user". I want to add a search box below and then search the gridview by "user" and then bind the data to a new grid view below. I am under strict instructions to have all data interactions passed via a web service.
I have a Web Method at the moment to get all the comments:
[WebMethod]
public DataSet SearchDatabase(string query)
{
DataSet ds = new DataSet();
string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dvd_forum.accdb;Persist Security Info=True";
string queryStr = "SELECT * FROM Comments WHERE User LIKE '%query%'";
OleDbConnection myConn = new OleDbConnection(database);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(queryStr, myConn);
myConn.Open();
myDataAdapter.Fill(ds, "Comments");
myConn.Close();
return ds;
}
Here's the front code:
<asp:Label ID="Label3" runat="server" Text="Search"></asp:Label><asp:TextBox ID="TextBoxSearch"
runat="server"></asp:TextBox><asp:Button ID="ButtonSearch"
runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:GridView ID="GridView2" runat="server">
The code behind:
protected void ButtonSearch_Click(object sender, EventArgs e)
{
string query = TextBoxSearch.Text;
localhost.Service1 myws = new localhost.Service1();
ds = myws.SearchDatabase(query);
GridView2.DataSource = ds;
GridView2.DataBind();
}
It just doesn't do anything at all, page refreshes with no new GridView or any other action.
On another side note:
I have another input on the same page with a required field validator that doesn't let me search unless I fill the input with some text. How can I resolve this?
Thanks.

string queryStr = "SELECT * FROM Comments WHERE User LIKE '%"+query+"%'";

Related

CheckedItems.Count Always Returns a Value of Zero (0)

I have Use telerik:radcombobox with mutiple select value.I have bind data LoadOndemand.All Work Fine but when i click on submit button then CheckedItems.Count=0.
Thank you,
Dhiren Patel
I believe you are using EnableLoadOnDemand property of the RadComboBox. RadComboBox items are not accessible on the server-side when loading them on demand and therefore always return CheckedItems as well as SelectedItems count as zero and this is a known issue. This is because RadComboBox items loaded on demand using the ItemsRequested event handler or WebService do not exist on the server and cannot be accessed using the server-side FindItemByText / Value methods. SelectedItem and SelectedIndex properties are always Null / Nothing. This is needed for speed (otherwise the combobox will not be that responsive upon each keypress if state information and ViewState were persisted).
Please have a look at the following code without using load on demand which works fine at my end.
<telerik:RadComboBox runat="server" ID="RadComboBox1" CheckBoxes="true">
</telerik:RadComboBox>
<br />
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Get Count" OnClick="RadButton1_Click">
</telerik:RadButton>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateRadCombobox("select ContactName from Customers");
}
}
protected void populateRadCombobox(string query)
{
String ConnString =
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ConnString);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(query, conn);
DataTable myDataTable = new DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
RadComboBox1.DataTextField = "ContactName";
RadComboBox1.DataSource = myDataTable;
RadComboBox1.DataBind();
}
finally
{
conn.Close();
}
}
protected void RadButton1_Click(object sender, EventArgs e)
{
if (RadComboBox1.CheckedItems.Count > 0)
{
//business logic goes here
}
else
{
}
Reference:
http://www.telerik.com/forums/checkeditems-count-always-returns-a-value-of-zero-0
http://www.telerik.com/forums/radcombobox-losing-client-selections-on-postback

Get submit button id for query

I have a master page in witch more then 30 button like (java,asp,php,sql,c,c++,etc...)
and a have a table of questions. All questions are tagged with one subject like asp,java,etc.
I want to get data according to the click button.
How is it possible?
protected void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from question", con);
int count = (int)cmd.ExecuteScalar();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
con.Close();
}
(I think one page for one button is not a good idea.)
There are few ways to capture button click inside Data such as Repeater, GridView, ListView.
Easiest way is to use Command event instead of Click event.
<%-- Let say this button is located inside Repeater control.--%>
<asp:Button ID="DoSomethingButton" runat="server" Text="Home"
OnCommand="DoSomethingButton_Command" CommandArgument="<%# Eval("Id") %>" />
// Retrieve e.CommandArgument from code-behind.
protected void DoSomethingButton_Command(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
}
If you want the ID of the button itself, you can cast the sender back to the Button and get it's ID.
protected void Button1_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string id = button.ID;
}
If I understand you question correctly you could select field from the table according to button information for example in this way. And you could use same event handler for different buttons. Hope this helps.
protected void Btn1_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string question = readData(button.Text);
//do further actions
}
private string readData(string subject)
{
string question = string.Empty;
using (SqlConnection con = new SqlConnection(/*connection_string*/))
{
using (SqlCommand cmd = new SqlCommand("SELECT question from question_table where subject = #subject", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#subject", subject);
con.Open();
object o = cmd.ExecuteScalar();
if (o != null){
question = o.ToString();
}
con.Close();
}
}
return question;
}

gridview displaying data with html tags how to remove html tags while showing in grid view

im inserting data into database with ckeditor like this <strong>krishna<br /> <br /> <em>hyderabad</em></strong>. but while displaying data in gridview its again coming with html tags, i want that should come in gridview with out html tags, itemdescription column having tags which i customized by fckeditor.
<asp:GridView ID="gvChallan" runat="server" AutoGenerateColumns="true">
</asp:GridView>
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kernelCS"].ConnectionString)
;
SqlCommand cmd = new SqlCommand("select itemsdescription,quantity,totalboxes from tbl_DeliveryChallan",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvChallan.DataSource = ds;
gvChallan.DataBind();
}
}

lisbox1.selected index changed to -1 autometically

I have a listbox in asp.net and I am binding list Items from database. I have a button named "submit". Now when pages loads I am getting desired values in listbox, but when I am selecting any value and pressing submit button it sets listbox's selected index to -1.
can anyone help me to get proper index value?
<div class="PageRight">
<asp:ListBox ID="ListOfSql" runat="server" SelectionMode="Single" DataTextField="sql_name"
DataValueField="sql_text" Style="margin-left: 0px" Width="205px" EnableViewState="true"
OnSelectedIndexChanged="ListOfSql_SelectedIndexChanged">
</asp:ListBox><br />
<asp:Button ID="btnPreSqlExe" runat="server" Text="Sumbit"
onclick="btnPreSqlExe_Click">
</asp:Button>
</div>
and .cs page is like
protected void btnPreSqlExe_Click(object sender, EventArgs e)
{
txtQuery.Text = ListOfSql.SelectedItem.Value.ToString();
}
To Bind Data in listBox I am using following Code
Private void loadSqlList()
{
if (!IsPostBack)
{
conn.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
string preSql = "select sql_name, sql_text from cn_sql_log order by sql_name";
OracleDataAdapter da = new OracleDataAdapter(preSql, conn);
da.Fill(ds);
ListOfSql.DataSource = ds;
ListOfSql.DataTextField = "Sql_Name";
ListOfSql.DataValueField = "sql_Text";
ListOfSql.DataBind();
ListOfSql.SelectedIndex = 0;
conn.Close();
}
and calling loadSqlList() on page_load

SelectedIndex auto select on page load

I have an account page where the user can view their account information.. I want them to be able to change their password here. The Way I have managed to implement it it is as follows:
Web service:
[WebMethod]
public string ChangePassword(DataSet ds)
{
string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dvd_forum.accdb;Persist Security Info=True";
OleDbConnection myConn = new OleDbConnection(database);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from Users", myConn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(myDataAdapter);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
myConn.Open();
myDataAdapter.Update(ds, "Users");
myConn.Close();
return "Password changed!";
}
Front code:
<asp:Label ID="username" runat="server" Text=""></asp:Label><span>'s Account</span><br />
<asp:TextBox ID="ChangePasswordInput" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Save"
onclick="ChangePassword_Click" />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label><asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" ErrorMessage="Required!" ControlToValidate="ChangePasswordInput"></asp:RequiredFieldValidator>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
back code:
public partial class Account : System.Web.UI.Page
{
public static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
username.Text = User.Identity.Name;
}
localhost.Service1 myws = new localhost.Service1();
ds = myws.GetUserAcc(User.Identity.Name);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void ChangePassword_Click(object sender, EventArgs e)
{
//change password
int i = GridView1.SelectedIndex;
ds.Tables["Users"].Rows[i]["password"] = ChangePasswordInput.Text;
GridView1.DataSource = ds;
GridView1.DataBind();
localhost.Service1 myws = new localhost.Service1();
Label2.Text = myws.ChangePassword(ds);
}
}
The problem with this is that I have to select the row in the gridview before changing the password. Is there any way I can have the row automatically selected as there will only ever be one row.. Or how can I code it differently to work without selecting the row first?
Thanks.
Your GridView and button are separate controls on the page, therefore you can't determine which user is selected for password change, without selecting the row in the GridView. IMO its better if you could put the button inside the GridView. Make an editable GridView.
Edit: Based on your comment that you will only have one row in your grid view, I really don't see the reason of using Gridview with datatable. You can have a label showing user name and textbox for new password. (You may wanna reconfirm the password). Then in your webservice, instead of passing datatable, you may pass the new password (its better if its encrypted) and then update the data using SQL Update statement. But if you still want to use the GridView then instead of getting the selectedIndex you can directly pass 0 in the Row index. You may check if the dataTable contains any row.
if(ds.Tables.Count > 0 && ds.Tables["Users"] != null && ds.Tables["Users"].Rows.Count > 0)
{
ds.Tables["Users"].Rows[0]["password"] = ChangePasswordInput.Text;
    GridView1.DataSource = ds;
    GridView1.DataBind();
    localhost.Service1 myws = new localhost.Service1();
    Label2.Text = myws.ChangePassword(ds);
}

Resources