Click here to Skip to main content
15,881,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

C#
using (SqlCommand cmd = new SqlCommand("SELECT Id, Location FROM Location"))
       {
           cmd.CommandType = CommandType.Text;
           cmd.Connection = con;
           con.Open();
           location.DataSource = cmd.ExecuteReader();
           location.DataTextField = "Location";
           location.DataValueField = "Id";
           location.DataBind();
           con.Close();
       }
       location.Items.Insert(0, new ListItem("--Select Location--", "0"));


Location Table
Id Location
1 US
2 UK
3 UAE


User Table
Id Name Location
1 XYZ 0
2 UXA 0

C#
cmd1.Parameters.AddWithValue("@Location", location.Text);



The ASP code is here

ASP.NET
<asp:DropDownList ID = "location" runat="server" CssClass="formcss" Width="100%"> </asp:DropDownList>




My Problem is that how do i store id of selected location into user table . By the given code i get 0 in all the row of inserted data . Please Help
Posted
Comments
j snooze 6-Aug-15 17:08pm    
Not much to go on here with what little code you provided and where you are calling it, but I'm going to take a wild shot in the dark and guess that you are rebinding your drop down to the data reader in the page load function of the webforms code behind. So every postback rebinds the drop down list and its always the first one. Try putting this around your databinding
if(!Page.IsPostback){
using (SqlCommand cmd = new SqlCommand("SELECT Id, Location FROM Location"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
location.DataSource = cmd.ExecuteReader();
location.DataTextField = "Location";
location.DataValueField = "Id";
location.DataBind();
con.Close();
}
location.Items.Insert(0, new ListItem("--Select Location--", "0"));
}

U have to bind the value of dropdownlist on postback.
C#
If(!IsPostBack)
{
LocationBind();
}
Public void Location()
{
cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            location.DataSource = cmd.ExecuteReader();
            location.DataTextField = "Location";
            location.DataValueField = "Id";
            location.DataBind();
            con.Close();
}

and also pass the value
C#
cmd1.Parameters.AddWithValue("@Location", location.SelectedValue);
 
Share this answer
 
v3
use this-
C#
cmd1.Parameters.AddWithValue("@Location", location.SelectedValue);
 
Share this answer
 
Comments
Mudreka Shabbir Hussain 7-Aug-15 3:02am    
I Tried this but didnt worked . I still get 0 in location Column
Er. Ajay Chauhan 7-Aug-15 4:41am    
did you try below to bind drop-down list within PageLoad method;
If(!IsPostBack)
{
LocationBind();
}
which is in 1st solution provided by Kiing_Makerr....??
If you do not use !IsPostBack condition in Page Load than before every server side event, drop-down selected value will be initialize with 0...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900