protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
public void GetData()
{
string SQL=string.Empty;
SqlDataAdapter commandToBeLog=null;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);
SQL = " SELECT * FROM tbl_Video";
if (txtSearchVideo.Text.Trim() != string.Empty)
{
SQL += " WHERE VideoName LIKE #VideoName ";
ListView1.DataSource = null;
}
SQL += " ORDER BY VideoID DESC";
DataSet ds = new DataSet();
SqlDataAdapter myCommand = new SqlDataAdapter(
SQL, con);
if (txtSearchVideo.Text.Trim() != string.Empty)
{
myCommand.SelectCommand.Parameters.Add("#VideoName", SqlDbType.NVarChar, 300).Value = "%" + txtSearchVideo.Text + "%";
}
myCommand.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind();
}
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.GetData();
}
protected void btnSerch_Click(object sender, EventArgs e)
{
if (txtSearchVideo.Text.Trim() != string.Empty)
{
GetData();
GetData();
}
else
{
GetData();
}
}
Hi, how do i make the list view paging is no depedent to the textbox search. I got a problem with listview search pager, whenever I click search button with textbox value equal to A, the btnSerch_Click event fire. It show out 3 pages data with results and then I navigate to page 3. But when I type in the textbox with value B(it suppose show one page data), and I didnt click search button, but i go to click page 2. The weird thing happen, it show not relevant data or broken data.
How do i solve this problem, i don't want the page navigate bar depedent to the textbox. What I mean is I click page 2, the GetData() fucntion not fire, but just the page trigger only.
You need to to remove this line
ListView1.DataSource = null;
from the GetData method. Because if you null the ListView, the properties you set in ListView1_PagePropertiesChanging will be lost.
And you can reduce the Button click method to just one line.
protected void btnSerch_Click(object sender, EventArgs e)
{
GetData();
}
Related
i have a 2 radiobutton on my webpage, they both have OnCheckedChanged event lik this
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
gender = "Male";
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
gender = "Female";
}
and i also have a dropdownlist which has OnSelectedIndexChanged event which populate an other dropdwonlist.
protected void Depid_SelectedIndexChanged(object sender, EventArgs e)
{
string id = Depiddrop.SelectedValue.ToString();
SqlCommand cmd = new SqlCommand("select StaffID, Name from Staff where Depid='" + id + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
empiddrop.DataSource = dt;
empiddrop.DataTextField = "Name";
empiddrop.DataValueField = "StaffID";
empiddrop.DataBind();
}
...the problem is that gender variable lose their value after postback (selectingitem from asp.net dropdownlist control). i also set the radiobutton property enableviewstate="true" but its not solving the problem.
..please help...and thanks
declare gender as static global variable
use something like
static string gender="";
use this as global variable
The following code is not saving the changes from the dataset to the database via the dataadapter.update(). I display the data on a winform to text boxes.
I have a save button that should save the changes made to the database. the changes are only saved to the in memory copy of the dataset. what am i missing to get this to save the changes to the database?
public partial class Frm_Main : Form
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
BindingSource binding_Login = new BindingSource();
SqlCommandBuilder builder = new SqlCommandBuilder();
SqlConnection connection = new SqlConnection();
SqlCommand sqlcommand = new SqlCommand();
public Frm_Main()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
this.Text = "Main (" + GlobalVars.username.ToString() + ")";
this.AcceptButton = btnSearch;
connection.ConnectionString = GlobalVars.sqlConnString;
}
private void FrmMain_Close(object sender, EventArgs e)
{
Close();
}
private void btnSearch_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(txtSearch.Text))
{
Search();
}
}
public void Search()
{
string sqlcommandstring = "select * from login where loginname like #search;";
connection.Open();
sqlcommand.CommandText = sqlcommandstring;
sqlcommand.Connection = connection;
sqlcommand.Parameters.AddWithValue("#search", "%" + txtSearch.Text + "%");
adapter.SelectCommand = sqlcommand ;
builder.DataAdapter = adapter;
adapter.Fill(ds,"Login") ;
BindControls();
txtLoginName.DataBindings.Add(new Binding("Text", binding_Login, "LoginName"));
txtPassword.DataBindings.Add(new Binding("Text", binding_Login, "Password"));
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.DeleteCommand = builder.GetDeleteCommand();
adapter.InsertCommand = builder.GetInsertCommand();
}
private void btnNext_Click(object sender, EventArgs e)
{
binding_Login.MoveNext();
}
protected void BindControls()
{
binding_Login.DataSource = ds.Tables[0];
}
private void btnPrevious_Click(object sender, EventArgs e)
{
binding_Login.MovePrevious();
}
private void btnSave_Click(object sender, EventArgs e)
{
ds.AcceptChanges();
adapter.Update(ds.Tables[0]);
}
}
I was able to resolve the issue by changing the save buttons click event to the following:
private void btnSave_Click(object sender, EventArgs e)
{
this.binding_Login.EndEdit();
adapter.Update(this.ds.Tables[0]);
}
The problem was in this line:
private void btnSave_Click(object sender, EventArgs e)
{
ds.AcceptChanges();//EDIT This is the problem!
adapter.Update(ds.Tables[0]);
}
I had a similar problem and during debbuging I realized that if you call .AcceptChanges() before DataAdapter.Update(), all your modified rows will change their status to Unchanged. This means that DataAdapter.Update() will lose all the flags it needs to pick the right INSERT, UPDATE, DELETE command.
I also had problems using editing batches like:
row.BeginEdit();
// Modify several rows
row.EndEdit();
As I understand, the problem here happens because all changes are reserved until you call the AcceptChanges() method, thus causing all the row state flags to be set as Unchanged, making DataAdapter.Update() essentially blind.
In short:
Create a DataAdapter.
Set the InsertCommand, UpdateCommand, DeleteCommand, SelectCommand.
Fill a DataSet, DataTable, DataRow[], with the adapter.
Make changes to your DataSet, DataTable, DataRow[].
Make sure these changes are flagged in the RowState property of the modified row(s).
To ensure this, don't use batch editing, and don't call AcceptChanges(), before the DataAdapter.Update() method.
Using the same adapter, call adapter.Update(DataSet|DataTable|DataRow[]).
I have a problem related to DataTable in .net.
i am trying to fill datatable on page_load .that part is ok no issue datatable correctly filled up with data. but I noticed that when I clicked button on page... it again try to fill datatable which just a time-consuming task... I want datatable should fill once only ,when , when site open in browser .. not at every click ... because I have a large amount of data in table so it takes time to load in datatable..
plz help me out ...
here is my code:
protectd void Page_Load(object sender, EventArgs e)
{
cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;
// if (this.IsPostBack == true)
{
dr1 = TableUtoP();
dtWordsList.Load(dr1);
}
}
OleDbDataReader TableUtoPunj(String ArrWord)
{
if (cnn.State == ConnectionState.Open)
{
cnn.Close();
}
cnn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "SELECT U, P from UtoP";
cmd.Connection = cnn;
OleDbDataReader dr = cmd.ExecuteReader();
return dr;
}
You need to check if the page is posting back or not, like this:
protected void Page_Load(object sender, EventArgs e)
{
cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;
if(!IsPostBack)
{
// This will only happen when the page is first loaded, as the first time is not considered a post back, but all others are
dr1 = TableUtoP();
dtWordsList.Load(dr1);
}
}
Only fill the dataTable if it's not a PostBack...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//TO DO: Make the call to fill the data table here
}
}
I have 2 comboboxes named cBTeam1 and cBTeam2 (winForm & C#) both are bound from same database table.
If a person selects a team from cBTeam1, I want this selected team to not be displayed in cBTeam2.
private void bindComboBox()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string queryTeam1 = "SELECT * FROM Teams ORDER BY Team_name";
SqlCommand cmd = new SqlCommand(queryTeam1, con);
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "Teams");
this.cBoxTeam1.SelectedIndexChanged -= new EventHandler(this.cBoxTeam1_SelectedIndexChanged);
cBoxTeam1.DataSource = ds.Tables["Teams"];
//if(cBoxTeam1.SelectedIndex
cBoxTeam1.DisplayMember = "Team_name";
cBoxTeam1.SelectedIndex = -1;
cBoxTeam1.ValueMember = "team_id";
this.cBoxTeam1.SelectedIndexChanged += new EventHandler(this.cBoxTeam1_SelectedIndexChanged);
}
and here is code for cBoxTeam2 event handler cBoxTeam2_SelectedIndexChanged
private void cBoxTeam2_SelectedIndexChanged(object sender, EventArgs e)
{
if (cBoxTeam1.SelectedIndex == cBoxTeam2.SelectedIndex)
{
MessageBox.Show("You already selected " + cBoxTeam2.Text);
}
team2_id = Int32.Parse(cBoxTeam2.SelectedValue.ToString());
}
For example cBoxTeam1 displaye 3 values i.e England, India, Austrailia.
if I Select India, after selection India shold not be display in the cBoxTeam2 combobox
Hey i have made sample for you.Sample have 2 combo box and bind same data source.And when I select combobox1 firstly check with list if exist bind combobox 2 except matched item.
protected List<string> lst
{
get
{
List<string> lst =
new List<string>();
lst.Add("1");
lst.Add("2");
lst.Add("3");
return lst;
}
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = lst;
comboBox2.DataSource = lst;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lst.Contains(comboBox1.SelectedItem.ToString()))
{
comboBox2.DataSource = lst.Select(q => q.ToString(CultureInfo.InvariantCulture)).Where(q => q.ToString() != comboBox1.SelectedItem).ToList();
}
}
Try this
public void loadcBTeam1()
{
....
string queryTeam1 = "SELECT Team_name FROM Teams where Team_name <> '" + yourComboBox2.text + "' ORDER BY Team_name";
......
}
and on your ComboBox2 TextChanged
private void comboBox2_TextChanged(object sender, EventArgs e)
{
loadcBTeam1();
}
I asked a similar question to this but the circumstances have changed.
I bind my gridview through code rather than on the source.
The pagination works fine, but if I click a button on second page of the gridview (after pagination), the postback is causing the pagination to reset to page 1. Can anyone tell me what I'm doing wrong?
Within my pageload i have set the !POSTBACK method as shown i.e if there is postback event, then it shouldn't reset the grid but it does!
Heres the onload:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["usersName"] != null)
{
object a = Session["_id"];
IDMaster = Convert.ToInt32(a);
GridView1.Columns[10].Visible = true;
GridView1.Columns[11].Visible = true;
}
else
{
GridView1.Columns[10].Visible = false;
GridView1.Columns[11].Visible = false;
}
if (!IsPostBack)
{
BindGrid();
}
The BindGrid();
SqlConnection sqlcon = new SqlConnection(connstring);
SqlCommand sqlcmd = new SqlCommand("select * from Coffees ORDER BY coffeeName ASC", sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Page index method:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if(ViewState["searchTerm"] != null)
{
object a = ViewState["searchTerm"];
string reloadTerm = a.ToString();
setGrid(reloadTerm);
}
You need to bind your gridview in GridView1_PageIndexChanging event
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if(ViewState["searchTerm"] != null)
{
object a = ViewState["searchTerm"];
string reloadTerm = a.ToString();
setGrid(reloadTerm);
}
BindGrid();
}
hopefully it works for you.
Since you are binding grid view dynamically, Please remove
if (!IsPostBack)
condition from page load. Grid view needs binding every time.
I found this issue. I forgot that after I add an item to my cart i was calling response.redirect to refresh the page....obviously this meant the page was recalled refreshing the page so the grid was always going to reset. Thanks again.