I have a sqldatasource connection in whose parameters, the insert parameter is set as
INSERT INTO [user_info] ([firstname], [lastname], [age]) VALUES (#firstname, #lastname, #age)
Now i understand #firstname, #lastname, #age are the parameters to which i set them the value.
I'm databinding it with a formview, which automatically binds the textbox in the insertitemtemplate with the columns Firstname, lastname and age respectively. For instance the FirstName Text box has the following property.
<asp:TextBox ID="firstnameTextBox" runat="server" Text='<%# Bind("firstname") %>' />
Now my doubt is that, the #firstname variable how is bound with the firstname field. I have not explicitly used the binding anywhere. Say tomorrow i want to rename the insert query as
INSERT INTO [user_info] ([firstname], [lastname], [age]) VALUES (#fn, #ln, #ag)
where and all i will have to make changes inorder to bind #fn with FirstName and so on.
Hope my question is clear.
Short Answer: changing your parameter names won't cause a ripple effect. Changing your column name will.
Detailed Answer: Databinding with Bind (and Eval) in this case applies to the column name of your SQL table, not the parameter name. If you were to start using the 2nd INSERT statement with #fn your Bind would continue to use "firstname" without needing to change.
However, if you updated your table and renamed the "firstname" column to "fn" this is no longer the case and you now have 2 choices:
Update all Bind() calls to use "fn" instead of "firstname" - this would require many changes if it occurs in many places.
Update your SELECT statement (or stored procedure) to alias the fn column as firstname (ie. SELECT [fn] as firstname, ... other columns ... FROM [user_info]) - this is advantageous since the change happens at the source and doesn't affect existing binding to "firstname," which means making a change in only one place.
Related
I am using DayPilot which has two attributes DataValueField and DataIdField. Both are assigned with some values. I can access DataIdField in Code behind using e.Value but cannot access DataValueField.
Are these same things or they are different ?
Please help
Same as my other answer:
As seen on this link the DataValueField is obsolete:
Gets or sets the name of the column that contains the id (primary key). Obsolete. Use .DataIdField instead.
In my project I can access the value this way:
DataValueField="id" //column "id" of my source table
Get it in BeforeEventRender:
string id = (string)e.DataItem["id"];
I have a drop down list inside of a DataList EditItemTemplate, fueled by a SQLDataSource (below). The parameter #panelid is being set in codebehind on the SQL databinding event, and that seems to be working fine. Contents are accurate and what I expected.
Then I tried setting the selected value using Bind("scopeid"), which should be fine. scopeid and equipmentid are related, in fact scopeid is populated by equipmentid in the footer of this same datalist, so their values should be matching (and they are). This should push the scopeid of the chosen record to the selected item in the ddl. However, I get the 'ddlEquipment' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value error when switching to my EditItemTemplate. Any ideas?
<asp:DropDownList class="smallInputddl" ID="ddlEquipment" runat="server" DataSourceID="sqlEditEquipment" SelectedValue='<%# Bind("scopeid")%>' DataTextField="modelnumber" DataValueField="equipmentid" AppendDataBoundItems="true">
</asp:DropDownList>
<asp:SqlDataSource ID="sqlEditEquipment" runat="server" OnDataBinding="sqlEditEquipment_DataBinding" ConnectionString="<%$ ConnectionStrings:ProductionDatabaseConnectionString1 %>"
SelectCommand="select * from tblsls_equipmentscope where proposalnumber in (select proposalnumber from tblsls_cntrlpanel where id = #panelid)">
<SelectParameters>
<asp:Parameter Name="panelid" />
</SelectParameters>
</asp:SqlDataSource>
To demonstrate, here's a Sql query to show the relationship between scopeid and equipmentid. tblsls_cntrlvfd also has the id from tblsls_cntrlpanel.:
select e.equipmentid, c.scopeid from tblsls_equipmentscope e
left join tblsls_cntrlvfd c on e.equipmentid = c.scopeid
where proposalnumber in (select proposalnumber from tblsls_cntrlpanel where id = 20)
Results:
equipmentid scopeid
----------- --------
9513 9513
9541 9541
9543 NULL
(3 row(s) affected)
The gist is that the equipment exists off in it's own little world, and controls in it's. Both get tied to a proposal. There's also a VFD that is associated to both a control panel and a specific piece of equipment.
You'll need to include a 'blank' option in your DDL for it to match the null value to. It's trying to find a match in the list to the cell's original value(null) but since that isn't an available option it's throwing the exception. Just set index 0 of your DDL to be blank.
Figured it out. I'm in the habit of keeping SQL data sources that just fuel a drop down list right with the list they populate. Usually this is desired. In this situation, keeping it with the list resulted in me having to use a work around to set it's parameter's value. It could not see the label it needed to from where it was, so I was setting the default value in the data sources Databinding event. This works fine on it's own, and the list populates as expected. As soon as you try to set the SelectedValue however, when the datasource is having it's parameters setup in databinding, what happens is a slight loading out of order issue. These eventually wind up running at the same time, so the datasource hasn't run and populated it's dataset yet, SelectedValue sees a set with 0 entries in it.
Moving the SQLDataSource out so that it could see the control I originally wanted it to solved the issue. It no longer needed to have it's parameter set through code, it's list is essentially static.
An error is thrown on a dropdown list that updates a table field. It claims the SelectedValue is invalid because it does not exist in the list of items. If I go into the table and add the zero to the ID the error is no longer thrown.
The dropdown selects a teacherid which is the primary key from the Teacher table and saves the ID to the Classes table. The classes table saves any IDs that begin with zero without the leading zero.
So if the tid is 0701234 in the Classes table it is saved as 701234 but both tables are nvarchar(255). I might add that the Classes table was using an int datatype for the tid. so I changed it to avoid this. The error above is still thrown after the change, since you cannot update a record that does not exist.
Here is the control
<asp:DropDownList ID="teacherdd1" runat="server" AutoPostBack="true"
DataSourceID="SQLdatasourceTeacherList"
DataTextField="fullname1" DataValueField="tid" >
</asp:DropDownList>
I simply do not know what to do, or why this is happening.
quite simple. i switched to a textbox to test and forgot to double check this. my oversight.
thanks
cmdUpdate.Parameters.Add("#TID1", SqlDbType.Int).Value = teacherdd1.SelectedValue.ToString()
cmdUpdate.Parameters.Add("#TID2", SqlDbType.Int).Value = teacherdd2.SelectedValue.ToString()
'cmdUpdate.Parameters.Add("#tid1", SqlDbType.NVarChar).Value = txbteacher1.Text.ToString()
'cmdUpdate.Parameters.Add("#tid2", SqlDbType.NVarChar).Value = txbteacher2.Text.ToString()
As MRAB already said, you need to follow this data through your code, to make sure it is treated as a string throughout. You mentioned that the field in the classes table used to be an int, which would suggest that int variables may also have been used in the code (for example the parameters to your SQL command?).
So just follow the data, either by stepping through with a debugger, or just following the code. Using the debugger would allow you to check that the data still contains the leading zeroes so that nothing unexpected is happening.
If the above doesn't help, then you will have to provide more of your code in the question, so that someone else might spot what is going on.
I have a combobox (DropDownList) control in my aspx project, linked to a datasource. I would like to show more than one field on the dataTextField (like in MS-Access). It's possible? How can I do it?
You can't specify more than one DataTextField. You need to figure out how to get the fields combined before databinding.
Generally, I accomplish this by modifying the query from the database. For example, if I wanted a full name of an employee in a drop-down list, I'd use the query
Select EmployeeNumber, FirstName + ' ' + LastName AS FullName from EmployeeTable
And set the DataTextField to FullName
If you don't have the flexibility to modify the query (for example, if you don't have rights to the database and are using a stored procedure set up by a DBA), you'd need to instead populate the drop-down in the code-behind. Perhaps have the query fill a DataTable, add a column to the DataTable, and set the newly added Column's value to be the FirstName + ' ' + LastName and set the DataTextField accordingly.
However, the first solution is better in my opinion because it's easier to change teh query (or stored procedure) than to change the code, recompile, and deploy if it needs to change down the road.
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key (FK) linking the two tables:
1) Table "Order"
OrderID, int, PK
AddressID, int, FK
...
2) Table "Address"
AddressID, int, PK
City, nvarchar(50)
...
Then I've created an (ADO.NET) Entity Data Model out of those two tables.
Now on my (ASP.NET) webpage I put a GridView with an EntityDataSource. In my GridView I want to display two columns:
OrderID
City (belonging to that order and linked by the AddressID-key)
How can I do that? My problem is: When I configure the Entity Data Source I can select an "EntitySetName" which can be
either "Order" or "Address" but not both, nor can I select any kind of relationship. If I select "Order" as EntitySetName
then in the GridView I can add the columns
OrderID
Address
Address.AddressID
Adding the column "Address" displays empty cells. Adding "OrderID" and "Address.AddressID" displays the expected IDs. But how can I
add the "City" of the related address to my GridView?
Thank you for help in advance!
Edit: Clarification:
The Entity Framework has created a class "Order" and a class "Address" corresponding to the database tables. The class "Order" has a reference to an "Address" object as a navigation property, corresponding to the 1-n relationship between Address and Order table.
Basically I want to have a column in my GridView which displays Order.Address.City. I have tried to add a bound field with "Address.City" as data field to the GridView but it results in a runtime error ("no such property...").
OK, much too many hours later I found the solution myself:
Option 1:
It is possible to use the select property in the EntityDataSource which allows to create arbitrary projections of data from several related entities/database tables (in my case: OrderID from Order entity and City from the
Address entity)
Drawback: Using select in the EntityDataSource makes using Insert, Update and Delete in the GridView impossible!
Option 2:
The EntityDataSource must have the include property to include the related address property along with the queried orders. The markup looks likes this:
<asp:EntityDataSource ID="EntityDataSourceOrders" runat="server"
ConnectionString="name=MyEntitiesContext"
DefaultContainerName="MyEntitiesContext"
EntitySetName="Order" Include="Address"
EnableDelete="True" EnableInsert="True"
EnableUpdate="True">
</asp:EntityDataSource>
Then the columns collection of the GridView can have a template field like this:
<asp:TemplateField HeaderText="City" >
<ItemTemplate>
<asp:Label ID="LabelCity" runat="server" Text='<%# Eval("Address.City") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Here Eval is important. Bind does not work. Also using a BoundField as a column...
<asp:BoundField DataField="Address.City" HeaderText="City" />
... is NOT possible. So it is not possible in the GridView to edit the City (which makes sense because its a related field belonging to another table and perhaps to many other orders). But it is possible to edit flat fields of the order entity and also the "AddressID" of the order to assign another address.