Search on gridView does nothing - c#

I have a gridVIew which has a datasource of a table in Access database.
I want to add search functionality on this gridview. I added a textBox and search button. My Code is
protected void btnSearchService_Click(object sender, EventArgs e)
{
string SearchField = TextBox1.Text;
string searchSQL = "SELECT * FROM LocalService WHERE ServiceName LIKE '%" + SearchField + "%'";
SqlDataSource1.UpdateCommand = searchSQL;
SqlDataSource1.Update();
}
But in browser when I click Search button, I see nothing happens. DO I have to add code on page load or somewhere?

It worked. I have replaced following lines
SqlDataSource1.UpdateCommand = searchSQL;
SqlDataSource1.Update();
with these lines
SqlDataSource1.SelectCommand = searchSQL;
GridView1.DataBind();

Related

select row from a gridview that during the execution has a new datasource

On the first column is the id and want to select it to be redirected to a page that has more details that row.
He goes good if datasource is not changed but if it is modified or receive ID of the initial data, or gives me error for that initially did not have many rows.
My code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.DataBind();
GridViewRow row = GridView1.SelectedRow;
string strCell = row.Cells[1].Text;
string myPageUrl = "Meci.aspx?ID=" + strCell;
Response.Redirect(myPageUrl);
}
Please tell me how can I change it to work well!

update contact info based on input fields through tsql

I have a web application that I am developing that has an 'my profile' page. This page grabs all the users info and populates the appropriates fields.
Certain details have been blacked out for privacy reasons. This is working great. What i want to achieve is the ability for the user to edit any of these text fields, and hit the 'update information' button. I am not sure how to do this.
Here is my code behind for what I have working so far.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grabData();
}
}
protected void grabData()
{
HttpCookie cookieLogin = Request.Cookies[Constants.CK_CAUSE_MARKETER_ID];
string sql = "SELECT * " +
"FROM CauseMarketers m with(nolock)" +
"INNER JOIN CauseMarketer_Contacts t with(nolock) ON t.CauseMarketerID = m.CauseMarketerID " +
"INNER JOIN CauseMarketer_Companies q ON q.CauseMarketerID = m.CauseMarketerID " +
"WHERE m.CauseMarketerID = " + cookieLogin.Value;
DataTable Results = Utils.SQLUtils.GetDataTable("TFOUNDATION", sql);
DataRow FirstResult = Results.Rows[0];
userFirstName.Text = FirstResult["FirstName"].ToString();
userLastName.Text = FirstResult["LastName"].ToString();
userCompanyName.Text = FirstResult["CompanyName"].ToString();
userAddress.Text = FirstResult["Address1"].ToString();
userAddress2.Text = FirstResult["Address2"].ToString();
userCity.Text = FirstResult["City"].ToString();
userZip.Text = FirstResult["ZipCode"].ToString();
}
Implement update information button click event.
<asp:Button ID="UpdateId" runat="server" Text="Update Information" OnClick="UpdateId_Click" />
If above the asp.net button then button client event look something like below.
protected void UpdateId_Click(object sender, EventArgs e)
{
// write update sql.
}
During first page load anyway information will get loaded based on your code. When user clicks on button this click event gets fired and you can capture all form information and send to db for update.

Gridview rebind data with new parameters

OK, so I need to change the values stored in my gridview based on input from user.
I used to filter it 100% with javascript, but it turns out that it needs to be paged, so that wont work. Instead, I have to call on the datasource again, but with parameters from the textbox.
I've figured out that I must call a function in code behind and from there call DataBind(), but I dont even know where to start. Please help
I know that I ought to post some code to show that I've made an effort, but I really dont have anything to show. i guess it would be something along these lines?:
protected void ReBind(string sParameter)
{
SqlDataSource.SelectParameters.Add("parameterName", sParameter);
myGridView.DataBind();
}
But obviously I'm fumbling in the dark here.
Take a update button
Write method for update function
public int update_method(string ParameterName)
{
module c = new module();
c.DB_Connection();
int i;
string QRY = "UPDATE TableName SET Parameter_Name='" + ParameterName + "' WHERE Parameter_Name='" + ParameterName + "'";
SqlCommand CMD = new SqlCommand(QRY, c.con);
i = CMD.ExecuteNonQuery();
return i;
}
on button click
protected void ButtonUpdate_Click1(object sender, EventArgs e)
{
update_method(ParameterNametxt.Text);
}
update_method(farm, common_obj);
Turns out that this worked out fine:
protected void ReBind(String sParameter)
{
SqlDataSource.SelectParameters.Remove(SqlDataSource.SelectParameters["parameterName"]);
SqlDataSource.SelectParameters.Add("parameterName", sParameter);
myGridView.DataBind();
}

Passing values from a class to a method of another class

I am creating a website. The home page has a text box and drop down box in which the user enters the movie name and language to search. When the user clicks on the Search button the search result page is displayed and the results of search should be displayed in a data grid. I created session variables to pass the text of the text box and data grid to be used in the other page. The code to fetch data from the database is in a class how do i pass the values received from the database to a method of another page? This is the code i have written, it doesn't give any errors but the data grid does not get filled with results what am I doing wrong?
//Code for search button in home page
protected void Btnsearch_Click(object sender, EventArgs e)
{
Response.Redirect("SearchResults.aspx");
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
}
//Code to fetch data from database
public class movie
{
public SqlDataAdapter searchmovie(object moviename, object language)
{
connection.con.Open();
SqlDataAdapter adapter1 = new SqlDataAdapter("select
select movieName,language,director,price from movie
where moviename = '" + moviename + "' and
language = '" + language + "'",
return adapter1;
}
}
//Code in search page to fill data grid with search results
public partial class SearchResults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
movie m = new movie();
SqlDataAdapter movieDetails = m.searchmovie(Session["moviename"],
Session["language"]);
DataSet data = new DataSet();
movieDetails.Fill(data, "movieD");
GridView1.DataSource = data.Tables["movieD"];
GridView1.DataBind();
}
}
Set the session variables before redirecting, like this:
protected void Btnsearch_Click(object sender, EventArgs e)
{
Session["moviename"] = TextBox3.Text;
Session["language"] = DropDownList1.Text;
Response.Redirect("SearchResults.aspx");
}
I would like to suggest to avoid Session as data storage.
ASP.NET has a nice feature called cross-posting: it make you able all the page control and state from a page to another.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Personally, I really love feature because you can refer to page as object, having controls exposed ad property!

textbox value remains unchanged on form submission in edit profile page

i am trying to create a edit profile page so when the page loads first i am filling all text boxes values as the value present in the database for the particular user.i am doing this using the code:
DBconn obj1 = new DBconn();
String sql1 = "select* from users where user_id='" + Session["user_id"].ToString() + "'";
DataTable dt = new DataTable();
dt = obj1.getdatatable(sql1);
if (dt.Rows.Count > 0)
{
referal_id.Text = dt.Rows[0]["referal_id"].ToString();
name.Text = dt.Rows[0]["name"].ToString();
password.Text = dt.Rows[0]["password"].ToString();
email.Text = dt.Rows[0]["email"].ToString();
mobile.Text = dt.Rows[0]["mobile"].ToString();
city.Text = dt.Rows[0]["city"].ToString();
state.Text = dt.Rows[0]["state"].ToString();
pincode.Text = dt.Rows[0]["pincode"].ToString();
sec_ques.Text = dt.Rows[0]["securityq"].ToString();
answer.Text = dt.Rows[0]["answer"].ToString();
age.Text = dt.Rows[0]["age"].ToString();
gender.Text = dt.Rows[0]["gender"].ToString();
user_id.Text = dt.Rows[0]["user_id"].ToString();
address.Text = dt.Rows[0]["address"].ToString();
date_of_joining.Text = dt.Rows[0]["date_of_joining"].ToString();
user_type.Text = dt.Rows[0]["user_type"].ToString();
}
now this is filling all the text boxes with the values .
when user edits some values in some textbox and resubmits the form the database is getting updated implementing the code:
protected void LinkButton1_Click(object sender, EventArgs e)
{
int i;
DBconn obj2 = new DBconn();
String sql2 = "update users set name='"+name.Text+"',address='"+address.Text+"',age='"+age.Text+"',gender='"+gender.Text+"',email='"+email.Text+"',mobile='"+mobile.Text+"',securityq='"+sec_ques.Text+"',answer='"+answer.Text+"',city='"+city.Text+"',state='"+state.Text+"',pincode='"+pincode.Text+"' where user_id='"+Session["user_id"].ToString()+"'";
i = obj2.executeDML(sql2);
if (i > 0)
{
Label1.Visible = true;
Image2.Visible = true;
Label1.Text = "Updated successfully!";
}
else
{
Label1.Visible = true;
Label1.Text = "Oops Update was unsuccessfull!";
}
}
the problem is the database is getting updated with the previous values which was already present in the database.when i used watchpoints i found the sql statement gets loaded with the textbox values which i binded using the above database code but its not fetching the values which the user edits.
plz help.
Hey Robin,
Please check whether the code is placed under the !IsPostback condition or not.
Please bind the values under this if loop.
if(!IsPostback)
{
// Bind values
}
do this on the page load.
The values must only be bound for the first time the page is load.
In your case it seems the values to the text boxes are bound for each and every time it is loaded(even in the postback also the values are loaded.)
So, in page load you must check the condition whether the page is postback or not.
Thus add this condition while binding the values in page load.
if(!IsPostback)
Firstly check the location from where u r calling the mathod for binding values...
put it on
Page.isPostback
so it will not change the value at posting time then when u will save it the value will change
the value must be changing when u click on button and page is again loading.

Categories