Gridview selected value always returns 0? - asp.net

I am trying to delete a row from a gridview if "Web drop course" option is selected. Here is the UI:
And the code:
for (int i = 0; i < showCourses.Rows.Count; i++)
{
if (((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue == "1")
{
dropList.Add(showCourses.Rows[i].Cells[2].Text +showCourses.Rows[i].Cells[3].Text );
}
}
Here is the dropdown list:
<asp:ListItem Selected="True" Value="0">No Action</asp:ListItem>
<asp:ListItem Value="1">Web Drop Course</asp:ListItem>
The problem is, ((DropDownList)showCourses.Rows[i].FindControl("actionmenu")).SelectedValue always returns 0 whether I choose No action or Web drop course. Can anyone see the problem?
Thanks

You are most likely not protecting against rebinding your data on postback. When your event that causes postback fires, the page load event fires before this. If you are binding in page load without a check for postback, you are basically resetting your data and then going into your event handler.
The page life cycle might be a good read: Page Life Cycle

Considering your previous post, you are rebinding the gridview on each postback. Wrap those lines with a !IsPostback conditional. Better wrap those into a method (say PopulateGrid()) and call it. Then, you can re-call that method in other situations where you might need to rebind the data (OnPageIndexChanged for example). Change your Page_Load method like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
string query = "select * from RegisterTable where StudentID='" + MyGlobals.currentID + "'";
SqlDataAdapter adap = new SqlDataAdapter(query, con);
DataTable tab = new DataTable();
adap.Fill(tab);
showCourses.DataSource = tab;
showCourses.DataBind();
}
}

Related

Filtered data refreshes to unfiltered data when clicking next page of web forms GridView

I have a web forms project that has one filter. When the user filters the data and clicks on the next page, the filter seems to get taken off and the default data is displayed. How can I fix this? Is it the OnPageIndexChanging attribute method that needs updating? I've shown the code behind below for the ONPageIndexChanging method and the method used to bind the data (had to cut some out to get rid of the "too much code" error but if I'm missing anything that would be helpful, please let me know.)
protected void dashboard_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
dashboard.PageIndex = e.NewPageIndex;
BindDataToGridView();
}
void BindDataToGridView(SqlCommand cmdSQL = null)
{
// default sql
if (cmdSQL is null)
{
cmdSQL = new
SqlCommand("SELECT TabID, TabName, Title, CreatedOnDate, TabPath From [tableName].[dbo].[Tabs] Order By TabName");
}
var connectionFromConfig = WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
using (cmdSQL)
{
cmdSQL.Connection = new SqlConnection(connectionFromConfig);
cmdSQL.Connection.Open();
DataTable rst = new DataTable();
rst.Load(cmdSQL.ExecuteReader());
dashboard.DataSource = rst;
dashboard.DataBind();
}
}
}
In place of your BindDataToGridView() for the paging event?
Call the SAME routine you have for the button click to filter the dates.
(you could even pull out the code for the button click into a separate routine. That routine filters by date, so have both your button click to filter, and the page index change event call that same routine.
So, for paging, you have to call the routine with the filter. This kind of suggests that you want one routine to load the grid, and it has to figure out if you have a filter or not. So, you might check for start/end date being blank. this would allow both page load event, the filter button, and the data page change event ALL to call ONE common routine.
So, a few things:
If you going to bind a grid/list view? and page? And filter?
(3 issues)?
Then make ONE common routine for all to call.
Say like this:
void BindDataToGridView()
{
SqlCommand cmdSQL = new SqlCommand("");
cmdSQL.CommandText = "SELECT TabID, TabName, Title, CreatedOnDate, TabPath From TableName Order By TabName";
if (startDate.Text != "")
{
// filter
cmdSQL.CommandText =
"SELECT TabID, TabName, Title, CreatedOnDate, TabPath From TableName " +
"WERE CreatedOnDate >= #Start AND <= #End ORDER By TabName";
cmdSQL.Parameters.Add("#Start", SqlDbType.Date).Value = startDate.Text;
cmdSQL.Parameters.Add("#End", SqlDbType.Date).Value = endDate.Text;
}
using (cmdSQL)
{
cmdSQL.Connection = new SqlConnection(conString);
cmdSQL.Connection.Open();
DataTable rst = new DataTable();
rst.Load(cmdSQL.ExecuteReader());
dashboard.DataSource = rst;
dashboard.DataBind();
}
}
So now we are NOT passing the filter. You could I suppose also put the sql or the status of the filter into view state, and have the filter routine check this, but you better off to do the above.
In fact, I often suggest that we filter the reocrdset against its built in "view", as that can save database hits, but it don't matter.
So, when introducing filters? AND paging? You have to keep the above in mind.
So, now we have one routine for page load, the button click and the pager changing index code.

ASP.Net GridView Edit/Update/Cancel/Delete Events On RowClick

We've got an ASP.Net application that contains a GridView Control that contains row edit functionality.
This allows a user to Edit, Delete, Or Cancel editing on a particular row.
For Example Read Only Mode Looks Like This:
And Edit Mode Looks Like this:
The mechanism that allows the user to enter Edit mode is based on an Edit Button in a template column that changes the selected row from a read only row to an editable row using a RowEditing event something like this:
protected void grdOfMine_RowEditing(object sender, GridViewEditEventArgs e)
{
grdOfMine.EditIndex = e.NewEditIndex;
ReBindDataGrid();
}
Canceling is pretty much the opposite where we have a button click event that changes the row back to ready only mode:
protected void grdOfMine_RowEditing(object sender, GridViewEditEventArgs e)
{
grdOfMine.EditIndex = -1;
ReBindDataGrid();
}
(Apologies to those who are already familiar with this aspect of ASP.Net forms development.)
We've also created a footer row that allows a user to add a new row:
We're looking for a way to extend the ASP.Net GridView control do this without using the buttons to fire the events.
For example:
Allow a user to enter edit mode for a row, by clicking in a cell of any given row and update the selected record say, on an Enter keyboard input event (Instead of the Edit Button).
Delete a record say, on a delete keyboard input event (Instead of the Delete Button).
Add a record in a similar fashion (Instead of the Add Button).
We were attempting this functionality using Infragistics controls, however we had a very tough time getting these to work, so we decided not to use them.
Thanks in advance
I am working on asp.net gridview on webforms and using Gridview RowCommand method of GridView OnRowCommand event for the Buttons View, Edit & Update inside TemplateField.
if (e.CommandName == "EditContract") {
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
gvContract.SelectRow(rowIndex);
using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#RoleName", SqlDbType.Char).Value = Session["RoleName"];
cmd.Parameters.Add("#UnitName", SqlDbType.Char).Value = Session["UnitName"];
cmd.Parameters.Add("#ContrSerialNo", SqlDbType.Int).Value = SerialNo;
dAdapter = new SqlDataAdapter(cmd);
DataTable DtContract = new DataTable();
dAdapter.Fill(DtContract);
}
if (e.CommandName == "UpdateContract") {
lblMessage.Text = "";
lblFile.Text = "";
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#ContrNewRev", SqlDbType.VarChar).Value = ddNewOrRevised.SelectedValue;
cmd.Parameters.Add("#ContractTitle", SqlDbType.VarChar).Value = txtContractTitle.Text;
cmd.Parameters.Add("#FinancerID", SqlDbType.Int).Value = ddFinancer.SelectedValue;
}
This code is working fine when the page loads first time but has 2 problems after that. i.e. 1. It is editing and updating data when the page gets load for the first time but when I try to edit the same row or any other then it doesn't. I know it is because I have defined the !Page.IsPostback method on the Page_Load event but I do not know how to tackle this situation. 2. Problem is How to restrict the gridview row to update only that data whose row is selected?
Please suggest me a solution.
GridView Sample

Object reference not set to an instance of an object. This happens while adding checkboxlist control dynamically

Object reference not set to an instance of an object.
protected void cmdSave_Click(object sender, EventArgs e)
{
string strNames = string.Empty;
CheckBoxList Chkboxx = (CheckBoxList)PlaceHolder1.FindControl("Chkbox");
foreach (ListItem em in Chkboxx.Items) //-------- (Showing error)
{
if (em.Selected)
{
strNames += em.Value + ", ";
}
}
string final_name = strNames.Substring(0, strNames.Length - 2);
lblNames.Text = final_name;
}
Actually I am adding Checkbox control dynamically :
protected void ddl_varient_SelectedIndexChanged1(object sender, EventArgs e)
{
string query = "select prd_vrtyvalue_id,varient_value from tbl_ProductVariety_Value where varient='" + ddl_varient.SelectedItem.Text + "' " +
" order by varient_value asc ";
DataTable abc = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(query, new CommonClass().connection());
ada.Fill(abc);
ChkboxList.ID = "Chkbox";
for (int i = 0; i < abc.Rows.Count; i++)
{
ChkboxList.Items.Add(new ListItem(abc.Rows[i]["varient_value"].ToString(), abc.Rows[i]["prd_vrtyvalue_id"].ToString()));
}
ChkboxList.RepeatColumns = 2;
PlaceHolder1.Controls.Add(ChkboxList);
}
Can Anybody tell me, what exactly i am doing wrong !
The way ASP.NET WebForms work is that the entire page is re-built during each post back. So, I imagine this is what is occuring:
Page gets "built" and includes only controls defined within your ASCX/ASPX file.
User clicks on DDL_VARIENT checkbox and the ChkboxList is added to PlaceHolder1
Form is rendered back to the user so they can see ChkboxList
Save button is clicked, causing another postback.
Page is re-built, setting all the controls back to what is defined within your ASPX/ASCX code. This does not include ChkboxList.
Your code is hit, ChkboxList no longer exists and you get your problem.
To fix, you could re-add your ChkboxList on Page_Load depending on the value of your DDL_VARIENT checkbox. If I were you though, I'd be tempted to define the ChkboxList within your ASPX/ASCX code and then set the visibility of the list depending on the value of the DDL_VARIENT checkbox within Page_Load.
I should add, the entire of the above is dependant upon you using ASP.NET WebForms. If you're using MVC then it's probably wrong.

GridView as DataTable source sorts only for the first time

I implemented sorting on my GridView with a DataTable as DataSource by using code from this MSDN link. However, my grid sorts for the first time when I click any column, and after that it does not sort on clicking any other column.
Code in the PageLoad() event -
if (!Page.IsPostBack)
{
HView hv = new HView ();
DataTable HTable = new DataTable("hTable");
HTable = hv.FillTable();
Session["hTable"] = HTable;
GridView2.DataSource = Session["hTable"];
GridView2.DataBind();
}
Code in the Sorting event -
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable notesDT = Session["hTable"] as DataTable;
if (notesDT != null)
{
notesDT.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection);
GridView2.DataSource = Session["hTable"];
GridView2.DataBind();
}
}
Does anybody have an idea of what I may be doing wrong?
EDIT: I just realized this. If I select a particular row, I have another view that gets populated with details about that row. When I view some rows details first before trying to sort any columns, then sorting works perfectly fine, any number of times. However, if I try to sort before selecting a row, it works only once.
You are using the DataTable as DataSource in the sorting event, but you should use the sorted view instead. Sorting the view won't change the sort order of the data in the table, just the order in the view.
protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable notesDT = Session["hTable"] as DataTable;
if (notesDT != null)
{
notesDT.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection);
GridView2.DataSource = notesDT.DefaultView;
GridView2.DataBind();
}
}
Edit: Although that i've just noticed that you're using rhe same code from MSDN.
You could also try to create a new DataTable from the view:
GridView2.DataSource = notesDT.DefaultView.ToTable(true);
You don't need stored the data table into a session. Actually putting the entire data table into session is not a good idea at all. Any particular reason for that?

i have repeater with check boxes i wants to delete records when one or more check boxes are checked on button click event

i tried but its not working
codes are as follows
protected void Button_Click(object sender, CommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
foreach (RepeaterItem item in Repeater2.Items)
{
CheckBox chk = (CheckBox)item.FindControl("MyCheckBoxID");
if (chk.Checked)
{
SqlConnection cn1 = new SqlConnection("Data Source=192.168.1.64;Initial Catalog=arvind;User ID=sa;password=platinum50");
cn1.Open();
SqlCommand cmd = new SqlCommand("delete * from Employee_Login " ,cn1 );
}
}
}
}
the error is microsoft run time error-form name="Form1" method="post" action="Default.aspx" id="Form1" onsubmit="Check(this)" at this line and saying object is expected
You need to tell your SQL command the ID of the item you want to delete (you can store this in the CommandArgument property) otherwise it won't know which row to delete from the Employee_Login table.
Also, you also need to Execute your command - at the moment all you are doing is defining it.
And after you have executed your command, you need to rebind the repeater.
HTH.
EDIT:
PS. Take a look at this if you're still having problems: http://bit.ly/fnLFRx

Resources