Label text databind to a column alias with dot - asp.net

I have this Select statement:
SELECT recordID As [Zap.st.] FROM SomeTable
When I try to bind the result to a GridView with template fields using an asp:Label I get this error:
System.Web.HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Zap'
This is a sample GridView:
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("Zap.st.") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried:
Text='<%# Eval("[Zap.st.]") %>'
Text='<%# Eval("'Zap.st.'") %>'
but none of these seem to work.
Edit: To further clarify my problem:
My Select statement is a View on Sql Server, the results in my asp.net come in as [Zap.st.], there is no reference that this is [recordID]

Try this code :
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("recordID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I found a similar question on SO, this is the solution:
<asp:Label ID="lblZapStD" runat="server" Text='<%# DataBinder.GetPropertyValue(Container.DataItem, "Zap.st.") %>' />
And this is the original question with an accepted answer.

Related

Input String not in correct format error, only when ID column Visible Property is set to false

My page has a GridView for managing Factory Plants. I'm able to successfully edit the data in the Grid by clicking the edit link and then saving my changes upon clicking the update link. I want the ID column of the Grid to be hidden from the user, so I added the Visible Property to the Plant_ID column of the Grid and set it to false, the column is hidden, but I get an error: "Input string was not in a correct format" upon hitting the update link.
This is my Grid and SQL data Source:
<asp:GridView ID="GridPlants" runat="server" DataKeyNames="Plant_ID" AllowPaging="True" PageSize="7"
AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True"
BorderWidth="2px" CellPadding="2" CssClass="datatable" GridLines="None"
ShowFooter="True" SortedAscendingCellStyle-CssClass="sortasc" OnSorting="gvPlant_Sorting"
SortedAscendingHeaderStyle-CssClass="sortasc"
SortedDescendingCellStyle-CssClass="sortdesc"
SortedDescendingHeaderStyle-CssClass="sortdesc"
OnRowCancelingEdit="GridPlants_RowCancelingEdit"
OnRowEditing="GridPlants_RowEditing" OnRowUpdating="GridPlants_RowUpdating" OnPageIndexChanging="gvPlants_PageIndexChanging"
>
<EditRowStyle />
<PagerStyle CssClass="pager-row" />
<RowStyle CssClass="row" />
<Columns>
<asp:BoundField DataField="Plant_ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="Plant_ID" Visible="false" />
<asp:TemplateField HeaderText="Plant Name" SortExpression="Plant_Name">
<EditItemTemplate>
<asp:TextBox ID="Txtplant" runat="server" Text='<%# Bind("Plant_Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Plant_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plant Code" SortExpression="Plant_code">
<EditItemTemplate>
<asp:TextBox ID="Txtcode" runat="server" Text='<%# Bind("Plant_code") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Plant_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<EditItemTemplate>
<asp:TextBox ID="Txtaddress" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="Txtcity" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:TextBox ID="Txtstate" runat="server" Text='<%# Bind("State") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("State") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip" SortExpression="Zip">
<EditItemTemplate>
<asp:TextBox ID="Txtzip" runat="server" Text='<%# Bind("Zip") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Zip") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plant Email" SortExpression="Plant_Email">
<EditItemTemplate>
<asp:TextBox ID="Txtemail" runat="server" Text='<%# Bind("Plant_Email") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Plant_Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<EditItemTemplate>
<asp:CheckBox ID="Chkactive" runat="server" Checked='<%# Bind("Active") %>'
Enabled="false" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("Active") %>'
Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UpdateBy" HeaderText="Update By"
ReadOnly="True" SortExpression="UpdateBy" />
<asp:BoundField DataField="UpdateDate" HeaderText="Update Date" ReadOnly="True"
SortExpression="UpdateDate" />
</Columns>
<%-- <PagerSettings FirstPageText="«" LastPageText="»" Mode="NumericFirstLast"
PageButtonCount="7" />--%>
<SortedAscendingCellStyle CssClass="sortasc" />
<SortedAscendingHeaderStyle CssClass="sortasc" />
<SortedDescendingCellStyle CssClass="sortdesc" />
<SortedDescendingHeaderStyle CssClass="sortdesc" />
</asp:GridView>
<asp:SqlDataSource ID="PlantDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ShipmentNotification %>"
SelectCommand="select Plant_ID, Plant_Name, Plant_code, Address, City, State, Zip, Plant_Email, Active, UpdateDate, UpdateBy from Plant order by plant_code">
</asp:SqlDataSource>
This is my Update function:
Protected Sub GridPlants_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridPlants.RowUpdating
Dim Editrow As GridViewRow = GridPlants.Rows(e.RowIndex)
Dim PlantId As String = GridPlants.DataKeys(e.RowIndex)("Plant_ID")
Dim textplant As TextBox = Editrow.FindControl("txtplant")
Dim textcode As TextBox = Editrow.FindControl("txtcode")
Dim checkbox As CheckBox = Editrow.FindControl("chkactive")
Dim address As TextBox = Editrow.FindControl("txtaddress")
Dim city As TextBox = Editrow.FindControl("txtcity")
Dim state As TextBox = Editrow.FindControl("txtstate")
Dim zip As TextBox = Editrow.FindControl("txtzip")
Dim email As TextBox = Editrow.FindControl("txtemail")
'I get an error on the following line:
DL.AddUpdatePlants("Admin", "Edit", Editrow.Cells(1).Text, textplant.Text, textcode.Text, address.Text, city.Text, state.Text, zip.Text, email.Text, checkbox.Checked)
'CType(Editrow.Cells(2).Controls(0), TextBox)).
GridPlants.EditIndex = -1
BindData()
End Sub
...Snippet of the stored procedure:
Dim myCommand As New SqlCommand("SavePlantDetails", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("#editType", addupdate)
myCommand.Parameters.AddWithValue("#Plant_ID", plant_id)
myCommand.Parameters.AddWithValue("#Plant_Name", name)
myCommand.Parameters.AddWithValue("#Plant_code", code)
myCommand.Parameters.AddWithValue("#address", address)
myCommand.Parameters.AddWithValue("#city", city)
myCommand.Parameters.AddWithValue("#state", state)
myCommand.Parameters.AddWithValue("#zip", zip)
myCommand.Parameters.AddWithValue("#email", email)
myCommand.Parameters.AddWithValue("#username", username)
myCommand.Parameters.AddWithValue("#Active", active)
myCommand.ExecuteNonQuery()
myConnection.Close()
...I'm wondering if by hiding the Plant_ID field, is that throwing off how I have my columns arranged? Could I get some help with this issue please? I only get the error after added the Visible property to the Plant_ID column and setting it to True. Thanks in advance.
Your value for Editrow.Cells(1).Text may not be coming up with visible false.
You can use the DataKeyNames property of the grid view to retrive this value.
set it as follows:
<asp:GridView ID="GridPlants" runat="server" DataKeyNames="Plant_ID" /> // rest of gridview markup
retrieve it as follows:
Dim PlantId as String = GridPlants.DataKeys(e.RowIndex)("Plant_ID")
and use this in your update function.
The Concept:
Editrow.Cells(1).Text
or in General:
rows.Cells(index).Text can ONLY be used to read data from a <asp:BoundField> field.
If you have TemplateFields as in <ItemTemplate> and <EditItemTemplate>, You must use the FindControl() method.
Here you have set AutoGenerateEditButton="true", thus your first Column ( Cell Zero obviously ) will contain the edit buttons actually. Starting with 2nd Column ( Cell 1) will be your Fields as you defined in your GridView Markup.
CASE I::
When you have set Visible="true" for your first <asp:BoundField> field, then Cells(1).Text actually refers to the Text inside Your Bound Field which is correct.
CASE II::
However, when you set Visible="false" for your first <asp:BoundField> field, then Cells(1).Text NO more refers to the first BoundField, but actually it's the next TemplateField: Plant_Name. The Data/Text in case of <asp:TemplateField> Column isn't inside the Cells but inside the Controls placed in these TemplateFields. This is the reason you get an error.

EditItemTemplate values not getting read properly

I am trying to build a simple GridView.
I have a Products table and it contains ProductID and ModelName.
I am using LINQ to SQL
My GridView code is
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my datasource code is
<asp:ObjectDataSource ID="objData" runat="server" SelectMethod="GetAll" UpdateMethod="UpdateProduct"
DataObjectTypeName="TestLINQProto.tblProduct" TypeName="TestLINQProto.ProductsList"></asp:ObjectDataSource>
I have my update method written like this
[DataObjectMethod(DataObjectMethodType.Update,true)]
public void UpdateProduct(tblProduct product)
{
EshopDataAccess.UpdateProduct(product);//This will call a LINQ function to update the product
}
The issue is that the tblProduct that the UpdateProduct contain original values. It is not getting the updated values.
I think the issue is that you are using Eval method which only supports reading/fetching of data. Use Bind Method in your grid aspx code. Like this
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Bind("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'M'

I am getting this exception at runtime which says it cannot find a column 'M' in datasource, but i am not using 'M' anywhere. I am trying to bind data to dropdownlist inside gridview.
I need to do this in .aspx page rather than code behind.
Here's the code i am using:
<asp:GridView ID="grdDrpDownlistSample" runat="server" AutoGenerateColumns="false" DataSourceID="sqlDS1">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged In Status">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%# Eval("LoggedIn") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:DropDownList ID="drpSex" DataSourceID="sqlDS1" runat="server" DataTextField='<%# Eval("Sex") %>' DataValueField='<%# Eval("id") %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from Users"> </asp:SqlDataSource>
It's working now.
I have got this working by removing Eval from dropdownlist, as it was expecting just the column name not the Eval expression.

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

How to bind a label inside a gridview to another table?

I have a very standard Gridview, with Edit and Delete buttons auto-generated.
It is bound to a tableadapter which is linked to my RelationshipTypes table.
dbo.RelationshipTypes:
ID, Name, OriginConfigTypeID, DestinationConfigTypeID
I wish to use a label that will pull the name from the ConfigTypes table, using the OriginConfigTypeID and DestinationTypeID as the link.
dbo.ConfigTypes:
ID, Name
My problem is, I can't automatically generate Edit and Delete buttons using an Inner Join in my dataset. Or can I?
Here is my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CssClass="TableList"
DataKeyNames="ID" DataSourceID="dsRelationShipTypes1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" Visible=False/>
<asp:TemplateField HeaderText="Origin" SortExpression="OriginCIType_ID">
<EditItemTemplate>
<asp:DropDownList Enabled=true ID="DropDownList2" runat="server" DataSourceID="dsCIType1"
DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("OriginCIType_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OriginCIType_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Destination" SortExpression="DestinationCIType_ID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="dsCIType1" DataTextField="Name"
DataValueField="ID" SelectedValue='<%# Bind("DestinationCIType_ID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DestinationCIType_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So I did try to create my own edit and delete buttons, but kept receiving the error
"cannot find update method"
or something similar. Do I have to manually code the delete and update methods in my code-behind?
You have to tell either the ObjectDataSource what object to use or the SQLDataSource what stored proc to use. Use the "UpdateMethod" attribute.
You can use the code behind technique to mention about what are the method that could handle the update and delete function. This is the standard way to do that. You can either use the Sqldatasorce to describe the source.You can mention which all the tables used for inner join and can also use the sql query.

Resources