I have a dropdownlist populated with a list of locations. The datasource is an ObjectDataSource named objectDataSourcePlaces. The DataValueField is PlaceID. The DataTextField, the value I want the user to see, resides in another table.
I'm guessing I need to perform some sort of JOIN to get the display string from the second table. The question, how do I get the display string from the second table and use that in my dropdownlist?
Yes, you would join the tables on the DataValueField and select the text field.
For example:
Select T1.PlaceId, T2.Text as DTField
from Table1 T1
left join Table2 T2 on T1.PlaceID= T2.PlaceId
Where some condition
Additional Info
I see you're having trouble in ASP.net side. all you'd have to do is set the DataTextField to the text field you selected via the joined tables. In my example above you'd do something like this:
DataTextField = "DTField"
You can do this in code on Load or whenever it is needed or in the Markup as an attribute of the drop down. Hope this helps.
Related
Please if someone have an idea about what i'm searching about.
i'm develloping an application web with asp.net and sql server. I have used a gridview and i want to make every new user after login see the gridview clear in order to create its own values.
But The gridview must still related with the same table of the database.
I hope that you understand my problem and i'm sorry for my english.
Are the columns in the gird bound to columns of your users table or are they bound to it's own separate table?
If it's the latter, the table for the grid should have a column specifically for the users' id
for example, if the records for the gridview were held in table USER_GRIDVIEW_RECORDS:
ALTER TABLE USER_GRIDVIEW_RECORDS
ADD USER_ID INT NOT NULL;
This way you can set the DataSource of the grid to the result of the following query:
SELECT * FROM USER_GRIDVIEW_RECORDS
WHERE USER_ID = LOGED_IN_USER_ID
And write the records of the gridview back to the database with the following query.
INSERT INTO USER_GRIDVIEW_RECORDS (USER_ID, OTHER_COLUMN_NAMES, MORE_COLUMN_NAMES)
VALUES (LOGED_IN_USER_ID, 'Example value', 'another example')
If this isn't enough for you, give us some example code of how you query your server and about how you write the DataSet to the DataSource of your GridView
My Asp.net page has multiple textboxes and DropDownLists that populate from a selection in the Gridview. My problem is multiple, I have a keyfield in the gridview that is a uniqueidentifier used to insert, update, and delete records from my database. I do not want theis column to be seen, I have used a css
.hideCol
{
display: none;
}
But it isn't working, I also tried setting Visible = "False" and it will not populate the textboxes. I need to find out how to hide that column so the user doesn't see it.
My new problem is my DropDownLists consist of a sql query from one table and concatenating multiple columns.
SELECT [Column1] + ' | ' + [Column2]
FROM [Table1]
ORDER BY [Column2]
So my result looks like this is the DropDown:
123456 | Name
When I try and populate my DropDownLists and Textboxes from the selected row of my gridview I get an error on the DropDownList because it is not recognized as a value.
123456 is what is in the database I need it to populate based on those numbers but display in the format above? Does this make sense? any suggestions?
UPDATE:
AllowPaging="True" CellPadding="4" CssClass="td3" ForeColor="#333333">
I have a GridView where one of the columns/fields has checkboxes; the checkboxes are checked or unchecked, based on a sub-query from the code behind, while the container GridView is bound to a SqlDataSource. What I want to be able to do is that once the GridView is databound then make it sort by the state of checkboxes: All rows with checkboxes checked appear on the top of grid. Here is part of my GridView:
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" Checked='<%# ShowCheckMarks(DataBinder.Eval(Container.DataItem,"prodid").ToString()) %>' />
</ItemTemplate>
I am thinking that I can call the GridView's Ondatabound event and someone make it sort from there?
Thanks.
If that is the only row that you need to sort by why not just add an order by to the sql query you're using to pull the data?
You can save yourself a lot of work if you
store the state of checkbox in database
. That way you can easily set a sort expression on the template field.
Edit
You can do this on clientside via jQuery tablesorter but it might be a
bit too much of work Check this.
Again I think you can somehow manipulate the query to achieve this.
Try this
SELECT dbo.Products.*, dbo.products_recommended.* FROM dbo.Products INNER JOIN dbo.products_recommended ON (dbo.Products.prodid = dbo.products_recommended.prodid) WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid) AND (dbo.Products.prodid = " + itemid + " order by dbo.Products.prodid desc )"
Please note : Never use a
select a.* from YourTable a
This will select all columns from your table & may bring a serious problem later on. Only query the columns you want like
select a.column1,a.column2 from YourTable a
SELECT dbo.Products.prodid,
dbo.Products.itemtitle,
dbo.Products.itemnumber,
dbo.Products.image_1,
CAST(ISNULL(dbo.products_recommended.recommendedid, 0) as BIT) as recommendedid
FROM dbo.Products LEFT OUTER JOIN dbo.products_recommended
ON (dbo.Products.prodid = dbo.products_recommended.prodid)
WHERE dbo.Products.prodid IN (dbo.products_recommended.prodid)
AND (dbo.Products.prodid = #itemid)
The left outer join will make sure it pulls all items from the Products table while only pulling matching items from the products_recommended table. It will also convert the recommendedid to a BIT value which should work with the checkboxes
Like the good helpers here suggested, I needed to have main sql query to populate the GridView to sort the records, instead of a different query in the code behind. So now the Declarative SqlDataSource syntax is the following. This solves the problem.
I learned a few hew things from the feedback here. Thank you all!
SELECT DISTINCT Products.prodid,
Products.itemtitle,
Products.itemnumber,
Products.image_1,
CASE WHEN YESNO IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END AS CheckUncheck
FROM Products
LEFT OUTER JOIN (SELECT prodid_recommended as YESNO FROM products_recommended
WHERE #itemid IN (prodid) ) A
ON Products.prodid = A.YESNO
ORDER BY CheckUncheck DESC
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 a GridView bound to a DataView.
Some columns in the DataView's table are foreignkeys to related tables(f.e. Customer). I want to enable sorting for these columns too, but all i can do is sorting the foreignkey(fiCustomer) and not the CustomerName.
I have tried this without success(" Cannot find column ERP_Customer.CustomerName "):
<asp:TemplateField HeaderText="Customer" SortExpression="ERP_Customer.CustomerName" >
A tried also the DataViewManager, but i've a problem to detect the table to sort:
Dim daCharge As New ERPModel.dsERPTableAdapters.ERP_ChargeTableAdapter
daCharge.Fill(dsERP.ERP_Charge)
Dim viewManager As New DataViewManager(Me.dsERP)
viewManager.DataViewSettings(dsERP.ERP_Charge).RowFilter = filter
viewManager.DataViewSettings(dsERP.ERP_Charge).Sort = sort 'sort is the GridView's SortExpression
Me.GrdCharge.DataSource = viewManager.CreateDataView(dsERP.ERP_Charge)
I have to apply the sort on a distinct table of the DataViewManager, but this table would differ on the related tables.
I have bound the TemplateColumns in Codebehind in RowDataBound-Event f.e.:
Dim LblCustomer As Label = DirectCast(e.Row.FindControl("LblCustomer"), Label)
LblCustomer.Text = drCharge.ERP_CustomerRow.CustomerName 'drCharge inherits DataRow
What is the recommended way to sort a GridView on columns related to other tables? I could build a custom datatable with the customername instead of the foreignkey and bind this column to the TemplateField. But then my huge dataset in the model makes no sense anymore.
EDIT:
It seems that my question was not clear or too special.
Perhaps i can rephrase it in a more general term.
I have a model with a Dataset. I'm binding one Datatable(ERP_Charge) from it to my GridView(actually i take a Dataview from that Table). In this Datatable are columns that are related to other Datatables in the Dataset(relations are defined). When i want to make the grid sortable its no problem on the columns that belong to ERP_Charge. But the columns with foreign keys to other table could not be sorted because the Gridview shows f.e. not the CustomerID but the Customername. I get the Customername in RowDataBound.
Normally i would join the tables and add a Datacolumn for the Customername. Then i set this "virtual" Datatable as Datasource from the Gridview and i'm able to sort it. But i didnt want to create the datatables in the Page on the fly(it belongs into the model). Do i have to define it in the dataset-Designer? I thought it would be sufficient to define the relationships in the dataset.
UPDATE: i had solved my sorting problems as below.
Regards
UPDATE: i had solved my sorting problems in the following way: I changed my DataSet in the model and joined the related table to the Main-Table(ERP_Charge). I added the columns to the Datatable(f.e. CustomerName) that i want to sort. But i did that without saving it in the configuration wizard of the DataAdapter, because that would delete all Update-,Delete- and InsertCommands. Hence i changed only the SelectCommand with the one that was generated from the Wizard. Therefore i kept the benefit of my Datamodel with the ability to sort for columns of related tables. Maybe queries are a little slower now because of the joins but it works for me.