Go Back   SitePoint Forums > Forum Index > Program Your Site > .NET
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 7, 2009, 04:59   #1
mdoghman
SitePoint Enthusiast
 
Join Date: Oct 2008
Posts: 46
Dropdownlist ASP.Net With Database Question

Hello everyone.
I have been trying to do something with a dropdownlist but it is not working.
This is my default2.aspx code

ASP Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bestellen2</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button runat="server" ID="Button1" Text="Bestel Item" onclick="Button1_Click" />
        <asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="HTMLContent" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

This is my code behind code

Csharp Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Web.Configuration;
using System.Text;
 
public partial class _Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!Page.IsPostBack)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["MacDonaldsDB"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
 
            con.Open();
 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT Item.ItemNaam, ItemPrijs.PrijsItem FROM Item INNER JOIN ItemPrijs " +
            "ON Item.ItemNaam = ItemPrijs.ItemNaam "    ;
 
            DropDownList1.DataSource = cmd.ExecuteReader();
            DropDownList1.DataTextField = "ItemNaam";
            DropDownList1.DataValueField = "PrijsItem";
            DropDownList1.DataBind();
            con.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblInfo.Text = DropDownList1.SelectedValue;
        HTMLContent.Text = DropDownList1.Text;
    }
}

What I am trying to do here is to show in my Label "HTMLContent" some other value than only "PrijsItem" Which is the DataValueField of my dropdownlist.

I want to show the Item Name AND the price of the item.

As an extra thing I am trying to make something similair to ordering .. like when a customer orders multiple Items and then he can calculate the price of them all.

Thanks in advance
mdoghman is offline   Reply With Quote
Old Nov 7, 2009, 05:09   #2
USPatriot
SitePoint Enthusiast
 
USPatriot's Avatar
 
Join Date: Oct 2009
Location: Mid-West USA
Posts: 30
Quote:
Originally Posted by mdoghman View Post
Hello everyone.
I have been trying to do something with a dropdownlist but it is not working.
This is my default2.aspx code

ASP Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bestellen2</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:Button runat="server" ID="Button1" Text="Bestel Item" onclick="Button1_Click" />
        <asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="HTMLContent" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

This is my code behind code

Csharp Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Web.Configuration;
using System.Text;
 
public partial class _Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!Page.IsPostBack)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["MacDonaldsDB"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
 
            con.Open();
 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT Item.ItemNaam, ItemPrijs.PrijsItem FROM Item INNER JOIN ItemPrijs " +
            "ON Item.ItemNaam = ItemPrijs.ItemNaam "    ;
 
            DropDownList1.DataSource = cmd.ExecuteReader();
            DropDownList1.DataTextField = "ItemNaam";
            DropDownList1.DataValueField = "PrijsItem";
            DropDownList1.DataBind();
            con.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblInfo.Text = DropDownList1.SelectedValue;
        HTMLContent.Text = DropDownList1.Text;
    }
}

What I am trying to do here is to show in my Label "HTMLContent" some other value than only "PrijsItem" Which is the DataValueField of my dropdownlist.

I want to show the Item Name AND the price of the item.

As an extra thing I am trying to make something similair to ordering .. like when a customer orders multiple Items and then he can calculate the price of them all.

Thanks in advance
HTMLContent.Text = DropDownList1.Text;

Should be.....

HTMLContent.Text = DropDownList1.SelectedItem.Text;

And "EnableViewState="true"" needs to be in the declaration on the .aspx page so it maintains values on postback.
USPatriot is offline   Reply With Quote
Old Nov 7, 2009, 05:11   #3
mdoghman
SitePoint Enthusiast
 
Join Date: Oct 2008
Posts: 46
Thanks a bunch.

But what if I want to display more columns in more labels than only two?
What can I use other that DataValueField?

Thanks again.
mdoghman is offline   Reply With Quote
Old Nov 7, 2009, 05:21   #4
USPatriot
SitePoint Enthusiast
 
USPatriot's Avatar
 
Join Date: Oct 2009
Location: Mid-West USA
Posts: 30
Sure, something like this....

Code:
SqlCommand objCommand = new SqlCommand(Sql String, objConnect); 

SqlDataReader objDataReader = objCommand.ExecuteReader();

if(objDataReader.Read()) { 

     lblID.Text = objDataReader["ColumnName1"].ToString();
     lblName.Text =  objDataReader["ColumnName2"].ToString();
     lblPassword.Text =  objDataReader["ColumnName3"].ToString();

}


objDataReader.Close();
objConnect.Close();
USPatriot is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 20:19.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved