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">
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
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 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.
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 an ASP.NET 3.5 web form with a DropDownList bound to a table of company names in a database. I also have a data bound GridView which I would like to update with data from the database depending on the company name selected in the DropDownList, so that the SelectCommand for the GridView's SqlDataSource is:
SELECT Registration, Telephone, Profile FROM {CompanyName}_VehicleData
Where {CompanyName} is whatever is selected in the DropDownList. I've used the Command and Parameter Editor to create a ControlParameter pointing to the SelectedValue of the DropDownList, but I don't know how to write the SelectCommand query to concatenate the parameter to '_VehicleData'. Any thoughts.
If you are using a sqldatasource you could set the select command in the code behind.
<sqldatasource>.SelectCommand = "select registration, telephone, profile " & _
"from " & <dropdown>.selectedvalue & "_VehicleData"
<sqldatasource>.SelectType = SqlDataSourceCommandType.Text
#Colin: You said that the solution is writing a stored procedure that evaluates whether the concatenated value (DropDown.SelectedValue + "_VehicleData") maps to a real table using the INFORMATION_SCHEMA.TABLES view before injecting it into a SQL command. Can you please give a code snippet for the stored proc?