Sql Server Query DropDownList Selected Value - asp.net

I have a DropDownList that pulls values from a table in sql server. I want the dropdownlist to populate the list of values based on a selection by the user but also display the contents of the rest of my table in case they need to change something. For ex:
ddl1 has values of: 1, 2, 3, 4, 5 user selects 5, so the value displayed in ddl2 is five, but if you exand the ddl2 you will also see the values of one, two, three, four...
This is the code in my load event...
If Not IsPostBack Then
result = dal.dbConnect(DAL.dbType.SqlServer, ConfigurationManager.AppSetting("SQLServerConnection"))
If result = "Successful" Then
Try
dt = dal.ExecuteSelectStoredProc(DAL.dbType.SqlServer, "StoredProc1", "#obj1", DropDownList1.Text)
DropDownList2.DataSource = dt
DropDownList2.DataTextField = dt.Columns.Item(1).ToString
DropDownList2.DataBind()
Catch ex As Exception
End Try
End If
End If
Stored Procedure...
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[StoredProc1]
#obj1 nvarchar(4)
AS
BEGIN
SET NOCOUNT ON;
SELECT [Obj1], [obj2]
FROM [DB1].[dbo].[Code]
WHERE [obj1] = #obj1
END

You need to set the AppendDataBoundItems property to true to preserve the existing values in the DDL:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx
If you're looking to implement a cascading effect with the DDLs, take a look at this:
http://www.codersbarn.com/post/2007/07/21/Code-Snippet-Cascading-DropDownLists.aspx

In your SelectedIndex Change event method, write the following:
ddl2.SelectedValue = ddl1.SelectedValue;

I'm not clear why you want to have two drop-down lists, or what is in each.
If you simply want a dropdown list with all the values from a table, but with the value showing being a value previously selected, just say "ddl.SetSelectedValue(somevalue)".

Related

Dynamic Dropdown issue

Hello I am trying to implement a drop down in asp.net. The drop down is being loaded from database and is being binded with drop down. The list has various values and text. But I want to add the first option of "Please Select" which is disabled for selection at the top of drop down list. Also on binding the drop down I want to point to the value which user had previously selected so that's why I am using selectedValue option. the problem is if I add the first item as "please select" from code behind at item 0 and selectedvalue to the value stored in db it still shows me item zero" please select".
DropDownList ddlMinEdit = (DropDownList)currentGrid.Rows
[currentGrid.EditIndex].FindControl("ddlMinEdit");
ddlMinEdit.Items.Insert(0, "Select Please");
ddlMinEdit.Items [0].Attributes.Add("disabled", "disabled");
ddlMinEdit.DataSource = CList;
ddlMinEdit.DataTextField = "empid";
ddlMinEdit.DataValueField = "empname";
ddlMinEdit.DataBind();
ddlMinEdit.SelectedValue = defemp;
So any suggestion how do I add first item and at same time bind a list and point to selected value. thanks
The order that you are doing things doesn't seems correct. You should insert the "Select Please" after the data binding and setting the selected value:
DropDownList ddlMRCClinEdit = (DropDownList)currentGrid.Rows [currentGrid.EditIndex].FindControl("ddlMRCClinEdit");
ddlMRCClinEdit.DataSource = clinList;
ddlMRCClinEdit.DataTextField = "empid";
ddlMRCClinEdit.DataValueField = "empname";
ddlMRCClinEdit.DataBind();
ddlMRCClinEdit.SelectedValue = defemp;
ddlMRCClinEdit.Items.Insert(0, "Select Please");
ddlMRCClinEdit.Items [0].Attributes.Add("disabled", "disabled");

How to return unique index value of multiple items selected in listbox

I have a listbox in VB.NET that allows users to select multiple categories. I need to put these categories into a database and need the index value from the listbox of the categories as the database works off IDs. SelectedItems (with an s) does not work in the net application.
I have tried the following code:
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = CategoryListBox.SelectedIndex
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
When iterating through the loop the code that is:
courseCategory.CategoryID = CategoryListBox.SelectedIndex
works correctly the first time around.
On the second iteration of the loop, it goes to the next selected item however returns the index for the first selected value. How do I return the values of the other indexes selected?
It depends on what ID you need to pass to your database. If it is truly the index of the ListItem in the ListBox, then you would use:
courseCategory.CategoryID = CategoryListBox.Items.IndexOf(category);
That may not be the best approach however. Maybe the order of the ListItems changes, messing up your indexes. You probably want to store the actual CategoryID on each ListItem. The Value property works well for that. You set the column you want as the DataValueField like so:
<asp:ListBox ID="CategoryListBox" runat="server"
DataValueField="CategoryID "></asp:ListBox>
So as you are looping through each ListItem in the ListBox and checking if it is selected, just use it's Value property.
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = category.Value;
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
Fixed the code by deselecting the item in the listbox after it was successfully saved.
End Using
category.Selected = False
End If
This solved my problem.

Updating data from GridView

I have a Gridview where I join two tables, bringing in the data to be displayed
table 1 sid, schedule, stime, splace, stourid
table 2 tourid, tourname
Basically they are joined stourid to tourid. That is so I can show the tourname in the grid.
Now I want to edit this GridView, what do I need to do, for instance, if the user edits the tourname, that it'll be saved to the database? I assume an Update statement is need, but how does it link to the GridView?
Whats the best and neatest method?
Regards,
Create Store Procedure to Update both tables and call it in your Update Code...
Create Proc UpdateData
#sid int,
#schedule,
#stime,
#splace,
#tourname
as
Begin
declare #tourid int,
select distinct #tourid=tourid from table1 where sid=#sid
begin try
// update tabel1
update tabel2 set tourname=#tourname where tourid =#tourid
end Try
begin Catch
end Catch
End
Using LinqToSQL you can do something like (if there is a FK relation between the tables)
DatabaseDataContext data = new DatabaseDataContext();
Table1 row = data.Table1s.Where(t =>t.ID == selectedID);
row.Table2.tourname = newName;
data.SubmitChanges();
You would then want to rebind the grid to show the new data.

If 2 dropdown showing same value on selected index change

I have been asked question on this scenario.There are 2 dropdownlist populating same data from same table one at top and other at bottom.when Item in first dropdownlist get selected,same item is shown in second dropdownlist and second item get selected,same item is selected in first.That means it is continuous loop.How to handle this situation. Don't want to use query
select name from table where id = ddl1.selecteditem.value and execute it.
On selectedindex function of dropdownlist1,he wrote like this
ddl2.selectedItem.value = ddl.selectedItem.value
and second list
ddl.selectedItem.value = ddl2.selectedItem.value
Does it possible to select the value from these 2 statement?I done like this,but not working.
In order to get the same value in both dropdownlists you need to do this in your dropdownlist1_selectedindexchanged:
DropDownList2.Items.FindByValue(DropDownList1.SelectedValue).Selected = true;
The same in dropdownlist2_selectedindexchanged:
DropDownList1.Items.FindByValue(DropDownList2.SelectedValue).Selected = true;
This should keep the same value in both dropdownlists whenever they have been modified.

how can i set an unbound GridView column to invisible in code

Dim Application = From AL In db.AnnualLeave _
Where AL.Approval <> True _
Select LeaveID, EmpID, Name
GridView3.DataSource = Application
GridView3.DataBind()
after calling `GridView3.DataBind(), why do i still get
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
at this line of code GridView3.Columns(1).Visible = False yet the grid has rows and more than 2 columns. i found a thread about similar problem here http://forums.asp.net/t/1025678.aspx/1
Note that the Gridview columns have NOT been defined at design time.
`
You need to be careful where you place your code in asp.net. If you placed that GridView3.Columns(1).Visible = False
code in the wrong place at the wrong time then yes, it would throw an error.
I suggest reading up on ASP.NET Page Lifecyle

Resources