|
I am getting the following error while editing values in a GridView row.I am posting my code and error below.
Error:
'cmbGender' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomerData.aspx.cs" Inherits="CustomerData" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Code,Type" ShowFooter="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:DropDownList ID="cmbGender" runat="server" SelectedValue='<%# Eval("Gender") %>'>
<asp:ListItem Value="Male" Text="Male"></asp:ListItem>
<asp:ListItem Value="Female" Text="Female"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewGender" runat="server">
<asp:ListItem Value="Male" Text="Male"></asp:ListItem>
<asp:ListItem Value="Female" Text="Female"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewState" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("State") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<EditItemTemplate>
<asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton3" runat="server">AddNew</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CustomerData : System.Web.UI.Page
{
CustomersCls customer = new CustomersCls();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillCustomerInGrid();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbType");
if (cmbType != null)
{
cmbType.DataSource = customer.FetchCustomerType();
cmbType.DataBind();
cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbNewType = (DropDownList)e.Row.FindControl("cmbNewType");
cmbNewType.DataSource = customer.FetchCustomerType();
cmbNewType.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillCustomerInGrid();
}
private void FillCustomerInGrid()
{
DataTable dt = customer.Fetch();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
GridView1.DataSource = dt;
GridView1.DataBind();
int TotalColumns = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
GridView1.Rows[0].Cells[0].Text = "No Record found";
}
}
}
Please help me to solve this error.Thanks in advance
|
|
|
|
|
mudunurusv wrote: <asp:DropDownList ID="cmbGender" runat="server" SelectedValue='<%# Eval("Gender") %>'>
<asp:ListItem Value="Male" Text="Male"></asp:ListItem>
<asp:ListItem Value="Female" Text="Female"></asp:ListItem>
</asp:DropDownList>
Error must be here.
You have defined two values for this dropdown - Male & Female.
Based on the error, your datasource field "Gender" at some time has any other value than above two. May be empty! Check what all values are coming.
|
|
|
|
|
Hi All,
let's suppose
DataSet ds = new DataSet();
ds.Tables.Add(datatable1)
ds.Tables.Add(datatable2);
Repeater1.DataSource = ds;
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:GridView ID="GridView1" runat=server DataSource="?????" AutoGenerateColumns=true >
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
then what would be the DataSource="?????" in gridview.
Thanks,
|
|
|
|
|
zeeShan anSari wrote: Repeater1.DataSource = ds;
Is incorrect. If you have a System.Data.DataSet with multiple tables as a datasource, you must specify which table to bind to the control.
Either:
Repeater1.datasource = datatable1
Repeater1.datasource = ds.Tables[0]
Same thing for GridView too.
GridView1.datasource = datatable1
GridView1.datasource = ds.Tables[0]
Details here:
MSDN: Repeater.DataSource Property [^]
MSDN: BaseDataBoundControl.DataSource Property [^]
|
|
|
|
|
I don't think so.
should:
DataSource=Repeater1.DataSource
|
|
|
|
|
???
|
|
|
|
|
DataSource connect Database ,
Not table.
|
|
|
|
|
put ul li in it Repeater,
that can Meet your needs!You try!
|
|
|
|
|
you can't set to table for repeater datasource, set it as one, or if you need each of them, use view in sql database, or use union linq method, that get it in one source!
like this:
var ds = (table1)Union(table2).ToList();
repeater1.DataSource = ds;
|
|
|
|
|
Hi,
I have included a radiobutton on a gridview using HTML control. I tried getting the index of the row selected by the radio button, but I can't figure out how. I tried using the Request.Form["RadiobuttonName"] but it gives me the value on the cell not the row index. Here is the code When I click the button to retrieve the value.
protected void btnSelect_Click(object sender, EventArgs e)
{
string selectedValue = Request.Form["radSelect"];
int index = Convert.ToInt32(Request.Form["radSelect"]);
}
And here is .aspx page
<asp:GridView ID="grdAll" runat="server" OnRowCommand="MPS_Selected" AutoGenerateColumns="false" OnRowDataBound = "grd_rowdatabound">
<Columns>
<asp:BoundField DataField="ID" HeaderText = "" Visible="false" />
<asp:BoundField DataField="Name" HeaderText = "MPS Account Name" />
<asp:BoundField DataField="Industry" HeaderText = "Industry" />
<asp:BoundField DataField="SubIndustry" HeaderText = "Sub-Industry" />
<asp:BoundField DataField="Geo" HeaderText = "Geo" />
<asp:BoundField DataField="LocType" HeaderText = "Location" />
<%--<asp:buttonfield buttontype="Button" commandname="Select" headertext="" text="Select"/>--%>
<asp:TemplateField HeaderText="Select One">
<ItemTemplate>
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' />
<%--<asp:RadioButton ID = "rdSelect" runat="server" onclick = "RadioCheck(this)" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID")%>' />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Any help will do.
Thanks!
|
|
|
|
|
What you have given is not a server side control.
<input id="rdSelect" name= "radSelect" type="radio" value='<%# Eval("ID") %>' /> is a client side html control.
2 things:
1. Convert it into server control using runat="server" to access it on server side.
2. In button select, loop through all the rows of grid, use FindControl to find radiobutton control. See if it is selected, if so use the row and it's values
Again, using a radiobutton in gridview is little tricky and needs extra effort to group them together so that only one can be selected at a time. If you face issue, just look for article/question on it on this website itself (shared before with lots of users)
|
|
|
|
|
Thanks for the reply. I tried using literal, but I am getting an error Quote: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. in this line
output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
|
|
|
|
|
Franco Cipriano wrote: output.Text = string.Format(@"<input id=""RowIndex{0}"" type=""radio"" name=""radSelect"" value=""{0}"" checked=""checked"" />");
You forgot to mention the string to be replaced in {0}
Something like:
string.Format("Hi {0}", "there!");
|
|
|
|
|
Thanks so much! problem solved!
|
|
|
|
|
Good to know!
|
|
|
|
|
Actually im getting error when the below code is executed from unit test project...
where import is an xml file in the below code..
XmlTextReader importReader= new XmlTextReader(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import"));
the above code is present in one of the methods in different project(class library) which is used in test method in the unit test project
where as the above path is present in web project.
and the value of the
System.Web.Hosting.HostingEnvironment.MapPath("~/Content/Importcontent/import")
is null..ie unable to get the complete path of the file i guess bcos the hostingenvironment is not running(web project)
The above code is working fine when running from the webproject ie it returns the exact path.
whereas returning null value when running from test project.
please let me know how to fetch the file path(where file is present in web project) when running from test project.
|
|
|
|
|
Either you need to copy the file in bin/root folder of your test project to access OR give an absolute path of the file to access.
|
|
|
|
|
I have some objects I should add to ASP.NET application state. Of course, I can add the object as a whole. The class that object belongs to have some properties and all of them will be added with the object to the application state.
But, is there a way to mark some properties of the class to not be included in adding object to the application state? I've thought about writing attribute, but am not sure if it will work.
Thank you for any suggestion.
|
|
|
|
|
Hi All,
i would like to do (ASP.NET-AJAX with C# )an autocomplete search textbox(like Google search) that give the user to choose more than one value to research in DataGrid as follows :
1 | 2 | 3
Rome,Paris,New York
when the user insert the first value can have also the possibilities to insert the second value value and so on ... then the datagrid can show all result requested and preferably i should do it in C# .
DO you have any idea how i can do it??
Thanks so much for your attention
Cheers
|
|
|
|
|
You can use this control(Multiselect Dropdown for Web Applications[^]) coupled with AJAX calls to make it as an autocomplete list. Based on the text entered, filter the datasource of the checkboxlist and once the values are selected, use the selected values as a folter for your grid.
Try!
|
|
|
|
|
Thanks Sandeep,
it is a good example but i can not filter the checklist box meantime i am inserting the value in the textbox...anyway i am trying to repair this error if you have any suggestion i will be so gratefel.
Thanks a lot.
Cheers
|
|
|
|
|
What error?
You have to further customize what I have shared as per your need.
|
|
|
|
|
hi,
not error in the project on codeproject but in my project that meantime i insert the value in the textbox i want the checkboxlist work likes the Autocomplete but it doesn't that so ai m trying to complete this issue.
Thanks so much for your help .
|
|
|
|
|
Hello All,
I am very confused to choose among many ways to do a task efficiently. Please suggest me the best way to do the following..
All I want is to "search a sql-database and show up the result in a grid". Note that each row would have an image, a link and some text.
My main check points are
1) Ajax-style grid: On pressing next-page, only the grid should be updated not the whole page
2) Client side pagination: Since the search result might be huge, on each time pressing the next-page, I want to get only the specified rows from the server. NOT THE WHOLE SEARCH RESULT- WHICH WOULD SLO DOWN MY INITIAL GRID SHOWING
.....
Solution Options:::
I am new to web-programming. While browsing through I got the following points...But i am not able to put them all together and somehow i am not confident about it..very much confused what to use and what not to... please help!!!
1) asp.net ajax toolkit
2) use a simple way to create a table with many rows in client side using java script, and get the data form server using AJAX writing the all code of myself. But in this case i need to have a way of pagination if myself
3) etc (may be many other solutions exist)
Practically, there may be many pros and cons which I am unaware of. I am looking for something efficient and easy to use. May be less code.
technology: vs2008/2010, Ajax/Jquerry/JS, sql-db... since havent started teh project I am open to any technology which gives the best solution.
thanks in advance
|
|
|
|
|
hrishi321 wrote: I am looking for something efficient and easy to use. May be less code.
Based on the following parameters, my suggestions would be:
1. Use a DataGrid and not a simple table to avoid extra code
2. Implement server side pagination such that each page is fetched on-demand
3. Use client side techniques to update the grid - jQuery AJAX, ASP.NET AJAX, Callback, WebMethods, etc.
One mistake in your question: "Client-side pagination" => that means whole data in one go and on page change no data fetched from server. That would not be needed.
|
|
|
|