3 Replies - 3682 Views - Last Post: 12 October 2009 - 03:40 AM Rate Topic: -----

#1 ismailc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 14-September 07

Aspx in Notepad - populate a dropdown with SQL DB & query

Posted 09 October 2009 - 12:19 AM

Good , I need help.

I have a blank aspx page where I want to add a drop down down from a sql database and get selection of dropdwon value.

Coding in Notepad & not in Visual Studio

I tried this example http://www.webconcer...combo/combo.asp that uses javscript but it connect to an access db.

How can I in a notepad edited aspx page

1. connect to a sql db

2. Create & populate a dropdown

3. textbox to show selected dropdown value.

This is all I have in the page:

<%@ Control Language="c#" AutoEventWireup="True" Codebehind="MainMenu.ascx.cs" Inherits="FlowCentric.Net.Navigator.MainMenu" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register Assembly="Infragistics2.WebUI.UltraWebNavigator.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.WebUI.UltraWebNavigator" TagPrefix="ignav" %>
<meta name="vs_snapToGrid" content="False">


<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

Please Help

Is This A Good Question/Topic? 0
  • +

Replies To: Aspx in Notepad - populate a dropdown with SQL DB & query

#2 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Aspx in Notepad - populate a dropdown with SQL DB & query

Posted 09 October 2009 - 10:59 AM

This example shows using a combobox substitute a listbox if you desire or replace your listbox with a combobox. The useage is exactly the same. This example also assumes you have your connection string to your database stored in your web.config file.

this example also uses an oracle database. Substitute the oracle connection and oracle command for the corresponding SQLServer Commands for your code.

You will need to modify the SQL Query to call your database tables.

The useage is exactly the same.
I also use a label for display instead of a textbox.

Other than those exceptions.. which are trivial .. this code does exactly what you are asking for..


hope this helps!


public partial class EmployeeRetrieval : System.Web.UI.Page
{
	string connectionString = WebConfigurationManager.ConnectionStrings["MyOracle"].ConnectionString;
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!this.IsPostBack)
		{
			FillEmployeeList();
		}
	}

	private void FillEmployeeList()
	{
		cboEmployee.Items.Clear();

		// Define the Select statement.
		string selectSQL;
		selectSQL = "SELECT lname, fname, ssn FROM employee";

		// Define the ADO.NET objects.
		OracleConnection con = new OracleConnection(connectionString);
		OracleCommand cmd = new OracleCommand(selectSQL, con);
		OracleDataReader reader;

		// Try to open database and read information.
		try
		{
			con.Open();
			reader = cmd.ExecuteReader();

			// For each item, add the name to the displayed
			// list box text, and store the ssn in the Value property.
			while (reader.Read())
			{
				ListItem newItem = new ListItem();
				newItem.Text = reader["lname"] + ", " + reader["fname"];
				newItem.Value = reader["ssn"].ToString();
				cboEmployee.Items.Add(newItem);
			}
			reader.Close();
		}
		catch (Exception err)
		{
			lblResults.Text = "Error reading list of names. ";
			lblResults.Text += err.Message;
		}
		finally
		{
			con.Close();
		}
	}




good luck!
Aet

This post has been edited by Aeternalis: 09 October 2009 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 ismailc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 14-September 07

Re: Aspx in Notepad - populate a dropdown with SQL DB & query

Posted 12 October 2009 - 12:15 AM

Thnaks for helping but not winning. I'm editing in notepad - nothing happens, displays the code on page no drop down

[code]
<%@ Control Language="c#" AutoEventWireup="True" Codebehind="MainMenu.ascx.cs" Inherits="FlowCentric.Net.Navigator.MainMenu" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register Assembly="Infragistics2.WebUI.UltraWebNavigator.v8.1, Version=8.1.20081.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
Namespace="Infragistics.WebUI.UltraWebNavigator" TagPrefix="ignav" %>
<meta name="vs_snapToGrid" content="False">

public partial class EmployeeRetrieval : System.Web.UI.Page
{
string connectionString = WebConfigurationManager.ConnectionStrings["provider=sqloledb;data source=srv;initial catalog=Warehouse;user id=ic; password=ic"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
FillEmployeeList();
}
}

private void FillEmployeeList()
{
cboEmployee.Items.Clear();

// Define the Select statement.
string selectSQL;
selectSQL = "SELECT distinct processid from dbo.fcevent";

// Define the ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataReader reader;

// Try to open database and read information.
try
{
con.Open();
reader = cmd.ExecuteReader();

// For each item, add the name to the displayed
// list box text, and store the ssn in the Value property.
while (reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = reader["lname"] + ", " + reader["fname"];
newItem.Value = reader["ssn"].ToString();
cboEmployee.Items.Add(newItem);
}
reader.Close();
}
catch (Exception err)
{
lblResults.Text = "Error reading list of names. ";
lblResults.Text += err.Message;
}
finally
{
con.Close();
}
}
[code]
Was This Post Helpful? 0
  • +
  • -

#4 ismailc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 14-September 07

Re: Aspx in Notepad - populate a dropdown with SQL DB & query

Posted 12 October 2009 - 03:40 AM

Thank You All - I got it going

[code:]

<asp:DropDownList ID="DropDownList1" Runat="server" DataSourceID="SqlDataSource1" onselectedIndexChanged="button1_Click"
DataTextField="processid" DataValueField="processid" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
ConnectionString="data source=srv2; initial catalog=MIS; user id=ic; password=ic;"
SelectCommand="select distinct ProcessID from dbo.fcEvent"></asp:SqlDataSource>

<script runat="server">
protected void button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Value !="View Process")
{

Session["mytext"] = DropDownList1.SelectedItem.Value;
Response.Redirect("myAdvancedTracking.aspx");

}
}
</script>
[/code:]

Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1