protected void Page_Load(object sender, EventArgs e)
{
label.Text = System.DateTime.Now.ToLongDateString();
Bind_ddlCountry();//// call function on page load
}
public void Bind_ddlCountry() ////fill the counrty
{
conn.Open();
SqlCommand cmd = new SqlCommand("select countryname,countrycode from country", conn);
SqlDataReader dr = cmd.ExecuteReader();
ddlcountry.DataSource = dr;
ddlcountry.Items.Clear();
ddlcountry.Items.Add("--Please Select country--");
ddlcountry.DataTextField = "countryname";
ddlcountry.DataValueField = "countrycode";
ddlcountry.DataBind();
conn.Close();
}
protected void Button_Click(object sender, EventArgs e)
{
string strcon = "Data Source=WALEED-PC\\SQLEXPRESS;Initial Catalog=ORSfinal;Integrated Security=True";
SqlConnection con = new SqlConnection(strcon);
string insert = "spcandidate";
SqlCommand command = new SqlCommand(insert, con);
command.CommandType = CommandType.StoredProcedure;
SqlParameter country = new SqlParameter("country", ddlcountry.SelectedItem.Text);/// on button click i insert country name in db using stored procedure
SqlParameter city = new SqlParameter("city", txtcity.Text);
SqlParameter phone = new SqlParameter("phoneno", txtphone.Text);
command.Parameters.Add(country);
command.Parameters.Add(city);
command.Parameters.Add(phone);
try
{
con.Open();
command.ExecuteNonQuery();
command.Dispose();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
}
so this is the complete code what i want know to select the country name ,, like "Pakistan" or "any country" from dropdownlist and want to insert in db it always insert "--Please Select country--", in db,,
waleedshah
Member
10 Points
41 Posts
how to insert the bind data of dropdownlist in database
Nov 11, 2012 06:37 PM|LINK
i bind country name and code with dropdown like this:
SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select countryname,countrycode from country", con); SqlDataReader dr = cmd.ExecuteReader(); ddlcountry.DataSource = dr; ddlcountry.Items.Clear(); ddlcountry.Items.Add("--Please Select country--"); ddlcountry.DataTextField = "countryname"; ddlcountry.DataValueField = "countrycode"; ddlcountry.DataBind(); con.Close();and now when i insert country name in db like :
SqlParameter country = new SqlParameter("country", ddlcountry.SelectedItem.Text);it insert "--Please Select country--" in db,,
what will be the solution for this?
thanks in advance
Hunain Hafee...
Member
238 Points
639 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 11, 2012 06:50 PM|LINK
u need to use FindControl property ,
Bhaarat
Contributor
3433 Points
855 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 11, 2012 06:56 PM|LINK
you should put the binding code withing if(!IsPostback)
eg
in Page_load () event
{
if(!IsPostback)
{
///Binding code goes here
}
}
refer this for more info http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.71).aspx
All the best
Remember to click "Mark as Answer" on the post that helps U
http://ransandeep.blogspot.com
Alsaady
Contributor
2024 Points
417 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 11, 2012 07:01 PM|LINK
www.rtoosh.net
oned_gk
All-Star
36010 Points
7354 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 11, 2012 09:54 PM|LINK
Suwandi - Non Graduate Programmer
Veera Pallat...
Participant
769 Points
202 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 12, 2012 04:37 AM|LINK
Hi,
Pleaase find the links for the same
http://forums.asp.net/t/1285363.aspx
http://stackoverflow.com/questions/6625356/adding-a-default-value-in-dropdownlist-after-binding-with-database
Veera Pallati
pradeep shar...
Contributor
4004 Points
763 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 12, 2012 05:16 AM|LINK
u have to follow 2 steps
1. implement not post back first
2. put a requiredfield validator so that use can not submit the form with --Please Select country-- option
oned_gk
All-Star
36010 Points
7354 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 12, 2012 05:46 AM|LINK
<asp:DropDownList ID="ddlcountry" runat="server" AppendDataBoundItems="True"> <asp:ListItem>--Please Select country--</asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlcountry" ErrorMessage="Please select country!"> </asp:RequiredFieldValidator>if (!IsPostBack) { SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select countryname,countrycode from country", con); SqlDataReader dr = cmd.ExecuteReader(); ddlcountry.DataSource = dr; ddlcountry.DataTextField = "countryname"; ddlcountry.DataValueField = "countrycode"; ddlcountry.DataBind(); con.Close(); }SqlParameter country = new SqlParameter("country", ddlcountry.SelectedValue);Suwandi - Non Graduate Programmer
matifnadeem
Contributor
4954 Points
1154 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 12, 2012 06:45 AM|LINK
Hi Waleed Shah,
Bind you country list on not postback condition, then OnSelectedIndexChanged event save selected country name to database. Modify your code like this.
ASPX CODE : ----------- <asp:DropDownList ID="CountryDropDownList" runat="server" AutoPostBack="true" AppendDataBoundItems="true" DataValueField="CountryCode" DataTextField="CountryName" OnSelectedIndexChanged="CountryDropDownList_SelectedIndexChanged"> <asp:ListItem ID="-1" Selected="true">-- Select Country -- </asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ID="CountryValidtor" runat="server" ControlToValidate="CountryDropDownList" ErrorMessage="Country must be selected!" InitialValue="-1" /> CODE BEHIND : ------------- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillCountryList(); } } private void FillCountryList() { var connection = new SqlConnection("Your connection string path"); connection.Open(); var command = new SqlCommand("SELECT CountryName, CountryCode FROM Country", connection); var dataReader= command.ExecuteReader(); CountryDropDownList.DataSource = dataReader; CountryDropDownList.DataBind(); connection.Close(); } protected void CountryDropDownList_SelectedIndexChanged(object sender, EventArgs e) { int countryCode = Convert.ToIn32(CountryDropDownList.SelectedValue); string countryName = CountryDropDownList.SelectedText.ToString(); if (countryCode > 0) { // Save that country code or country name into database } }Cheers
M Atif Nadeem
Mark as Answer if you got right thing
Read my blog | Follow me on LinkedIn
waleedshah
Member
10 Points
41 Posts
Re: how to insert the bind data of dropdownlist in database
Nov 12, 2012 12:14 PM|LINK
@all, you people didn't understand my question,
i bind different coutry name from database like
protected void Page_Load(object sender, EventArgs e) { label.Text = System.DateTime.Now.ToLongDateString(); Bind_ddlCountry();//// call function on page load } public void Bind_ddlCountry() ////fill the counrty { conn.Open(); SqlCommand cmd = new SqlCommand("select countryname,countrycode from country", conn); SqlDataReader dr = cmd.ExecuteReader(); ddlcountry.DataSource = dr; ddlcountry.Items.Clear(); ddlcountry.Items.Add("--Please Select country--"); ddlcountry.DataTextField = "countryname"; ddlcountry.DataValueField = "countrycode"; ddlcountry.DataBind(); conn.Close(); } protected void Button_Click(object sender, EventArgs e) { string strcon = "Data Source=WALEED-PC\\SQLEXPRESS;Initial Catalog=ORSfinal;Integrated Security=True"; SqlConnection con = new SqlConnection(strcon); string insert = "spcandidate"; SqlCommand command = new SqlCommand(insert, con); command.CommandType = CommandType.StoredProcedure; SqlParameter country = new SqlParameter("country", ddlcountry.SelectedItem.Text);/// on button click i insert country name in db using stored procedure SqlParameter city = new SqlParameter("city", txtcity.Text); SqlParameter phone = new SqlParameter("phoneno", txtphone.Text); command.Parameters.Add(country); command.Parameters.Add(city); command.Parameters.Add(phone); try { con.Open(); command.ExecuteNonQuery(); command.Dispose(); } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { con.Close(); con.Dispose(); } }so this is the complete code what i want know to select the country name ,, like "Pakistan" or "any country" from dropdownlist and want to insert in db it always insert "--Please Select country--", in db,,
now how to solve this problem???