Another Issue with asp.net

Dropdown list submitting wrong value

Page 1 of 1

11 Replies - 1053 Views - Last Post: 01 September 2009 - 10:14 PM Rate Topic: -----

#1 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Another Issue with asp.net

Posted 30 August 2009 - 12:32 AM

Hi.

I have a dropdownlist thats generaqted from a dataset.
By default the first value is selected of course.

When I submit the form it keeps on sending through the first value (currently selected one) even though I have selected a different one.

See below the code for the dataset generator and the on_Click function.

The On_Click function
protected void AddNewArea_Click(object sender, EventArgs e)
	{

		string connString = "Data Source=.\\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true;";
		SqlConnection conn = new SqlConnection(connString);

		// INSERT Query
		string cmdString = "INSERT INTO LodgeAreas " +
							"( AreaName, pid) " +
							"VALUES('" + areaname.Text + "','" + area.SelectedItem.Value + "')";

		SqlCommand cmd = new SqlCommand(cmdString, conn);
		conn.Open();
		cmd.ExecuteNonQuery();
		conn.Close();

		//errmsg2.Text = cmdString;
	}



The Dropdown List:
public void getDropDown2()
	{
		SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true;");
		SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM content WHERE pid = '3'", conn);
		DataSet ds = new DataSet();
		da.Fill(ds);

		area.DataTextField = "pageTitle"; //Set the column name which is shown as dropdown text

		area.DataValueField = "id"; //this is for value

		area.DataSource = ds; //set the datasource here

		area.DataBind(); //bind the dataset
	}




tx

Is This A Good Question/Topic? 0
  • +

Replies To: Another Issue with asp.net

#2 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Another Issue with asp.net

Posted 30 August 2009 - 10:47 AM

View PostJ0K3R, on 29 Aug, 2009 - 11:32 PM, said:

Hi.

I have a dropdownlist thats generaqted from a dataset.
By default the first value is selected of course.

When I submit the form it keeps on sending through the first value (currently selected one) even though I have selected a different one.

See below the code for the dataset generator and the on_Click function.

The On_Click function
protected void AddNewArea_Click(object sender, EventArgs e)
	{

		string connString = "Data Source=.\\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true;";
		SqlConnection conn = new SqlConnection(connString);

		// INSERT Query
		string cmdString = "INSERT INTO LodgeAreas " +
							"( AreaName, pid) " +
							"VALUES('" + areaname.Text + "','" + area.SelectedItem.Value + "')";

		SqlCommand cmd = new SqlCommand(cmdString, conn);
		conn.Open();
		cmd.ExecuteNonQuery();
		conn.Close();

		//errmsg2.Text = cmdString;
	}



The Dropdown List:
public void getDropDown2()
	{
		SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true;");
		SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM content WHERE pid = '3'", conn);
		DataSet ds = new DataSet();
		da.Fill(ds);

		area.DataTextField = "pageTitle"; //Set the column name which is shown as dropdown text

		area.DataValueField = "id"; //this is for value

		area.DataSource = ds; //set the datasource here

		area.DataBind(); //bind the dataset
	}




tx


when u bind the dataset, try to be more particular by specifying the table in the dataset that u r using,
something lyk databindings.add.tables(0) or specify the data source as ds.tables(0)..
hope this helps.......

This post has been edited by aks29921: 30 August 2009 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Re: Another Issue with asp.net

Posted 30 August 2009 - 10:55 AM

see the data is generated correctly cause if I view source on the page this is the output.

<select name="ctl00$ContentPlaceHolder1$area" id="ctl00_ContentPlaceHolder1_area" class="TextInput">
	<option value="6">Botswana</option>
	<option value="7">Tanzania</option>
	<option value="8">Zambia</option>
	<option value="9">Kenya</option>
	<option value="10">South-Africa</option>

</select>



Thats why I dont understand if I submit regardless of what I select it keeps on sending through Botswana.. value6.
Was This Post Helpful? 0
  • +
  • -

#4 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Another Issue with asp.net

Posted 30 August 2009 - 11:50 AM

J,

Try creating a label that will store the selected value when it is changed, using:

	protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
	{
		hiddenLabel.Text = this.DropDownList1.SelectedItem.Value;
	}



You'll need to turn on autopostback to get the value, you can use a content template to do a async request.

Then on the insert try referencing the label text value on the postback. You also will want to fill the text label with whatever default value you want to start with.
Was This Post Helpful? 0
  • +
  • -

#5 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Re: Another Issue with asp.net

Posted 30 August 2009 - 12:15 PM

View PostW3bDev, on 30 Aug, 2009 - 10:50 AM, said:

J,

Try creating a label that will store the selected value when it is changed, using:

	protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
	{
		hiddenLabel.Text = this.DropDownList1.SelectedItem.Value;
	}



You'll need to turn on autopostback to get the value, you can use a content template to do a async request.

Then on the insert try referencing the label text value on the postback. You also will want to fill the text label with whatever default value you want to start with.


Hi W.

Tx for always helping..

I did tried the above.. its still posting back 6.

and not anything else.

im frustrated.... Thinking of going back to php... asp.net is confusing.

:angry:
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Another Issue with asp.net

Posted 30 August 2009 - 02:11 PM

can we see the page source(the HTML before you run it)?
Was This Post Helpful? 0
  • +
  • -

#7 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Re: Another Issue with asp.net

Posted 01 September 2009 - 09:59 AM

View Posteclipsed4utoo, on 30 Aug, 2009 - 01:11 PM, said:

can we see the page source(the HTML before you run it)?


here it is:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Admin" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table>
	<tr>
		<td>
			<table class="AdminTable">
				<tr>
					<td><asp:Label ID="errmsg2" runat="server" Text=""></asp:Label></td>
				</tr>
				<tr>
					<td>Title</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="title"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Region</td><td><asp:DropDownList CssClass="TextInput" ID="region" runat="server"></asp:DropDownList></td>
				</tr>
				<tr>
					<td>Description</td><td><asp:TextBox TextMode="MultiLine" Columns="50" Rows="15" runat="server" ID="description"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 1</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image1"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 2</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image2"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 3</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image3"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 4</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image4"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 5</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image5"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Image 6</td><td><asp:TextBox CssClass="TextInput" runat="server" ID="image6"></asp:TextBox></td>
				</tr>
				<tr>
					<td colspan="2"><asp:Button ID="AddNew" onclick="AddNew_Click" runat="server" Text="Add Lodge" /></td>
				</tr>
			</table>
		</td>
		 <td valign="top">
			<table class="AdminTable">
				<tr>
					<td>Name : </td><td><asp:TextBox CssClass="TextInput" runat="server" ID="areaname"></asp:TextBox></td>
				</tr>
				<tr>
					<td>Parent : </td><td> <asp:DropDownList CssClass="TextInput" ID="area" runat="server"></asp:DropDownList></td>
				</tr>
				<tr>
					<td colspan="2"><asp:Button ID="AddNewArea" onclick="AddNewArea_Click" runat="server" Text="Add Area" /></td>
				</tr>
			</table>
		</td>
   </tr>
</table>

<% //test(); %>


</asp:Content>



Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Another Issue with asp.net

Posted 01 September 2009 - 01:12 PM

Where in your code does the getDropDown2() method get called?

More than likely when it posts back it reloads the DDL and it will default to the first item.
Was This Post Helpful? 1
  • +
  • -

#9 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: Another Issue with asp.net

Posted 01 September 2009 - 02:23 PM

View PostJayman, on 1 Sep, 2009 - 12:12 PM, said:

Where in your code does the getDropDown2() method get called?

More than likely when it posts back it reloads the DDL and it will default to the first item.



If this is true, you can check for postback and only set the default when the page is not posted back.
Was This Post Helpful? 1
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Another Issue with asp.net

Posted 01 September 2009 - 02:27 PM

I agree with Jayman. I can almost guarantee you that in your "Page_Load" method, you call "getDropDown2()", similar to this...

protected void Page_Load(object sender, EventArgs e)
{
     getDropDown2();
}



if so, you need to change that to this...

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostback)
     {
          getDropDown2();
     }
}


Was This Post Helpful? 1
  • +
  • -

#11 NitinPro  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 25-August 09

Re: Another Issue with asp.net

Posted 01 September 2009 - 08:08 PM

hey dude,

Firstly why u don't create a connection from server at page load....and if u call getdown function than i will recommend u to call this function at page load so that when page loads all the values from the database retrieve in ur drop down list.

and also enabled the autopostback property so that when u select dropdownlist selected item,it randomnly post ur data to the server...

another method to simply store the selected item of dropdownlist in label..

hope u will try....:)

This post has been edited by NitinPro: 01 September 2009 - 08:12 PM

Was This Post Helpful? 1
  • +
  • -

#12 J0K3R  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 298
  • Joined: 19-December 08

Re: Another Issue with asp.net

Posted 01 September 2009 - 10:14 PM

Jayman , W3bDev, eclipsed4utoo , NitinPro

Thanks SO much!! It really sucks to start out with something new but the best way to learn is this way I guess. Struggling with it and then sorting it out..

Problem was that I had that set in my "Page_Load" so yes it did generate a new list ..
(Why I didnt think of this... )
I added the postback and now it works..

Thanks once again for the help!! Really appreciate it..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1