1
Reply

Dropdown Problem!!!

learner

learner

Dec 1 2009 1:26 PM
2.8k
I will explain my scenario now,its bit easy. I have a Registration form, and RegisterTbl and Citytable.I have many controls especially 2 dropdown called City and country.In Citydropdown 2 values(first and last) are default and other values are from databse.If I select the dropdownvalu = OTHER(default value),one txtbox should appear and post the the other city data into Citytble.But when form Submit the Registration form,all the fields should get posted to Register table.!i have written all the code,but the problem is first page load all the dropdowns populated well.But when i changed the selected item in dropdown,each recordsd are multiplying and getting duplicated ! And one more problem is after displaying the txtbox,b4 giving value to it,im getting sql exception as Cannot insert NULL into column City,(Actually I have have created one new column in City tbl as Othercity,so i have city,country and othercity columns in Citytble)!! Im stucked up..I dont know how to proceed?? I am writing my whole code ..Can anyone help me! using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Collections; namespace New_Login_Page { public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { txtOther.Visible = false; SqlCommand cmd = new SqlCommand("SELECT City FROM City ", new SqlConnection(ConfigurationManager.AppSettings["StrConn"])); SqlCommand cmd2 = new SqlCommand("SELECT Distinct Country FROM City ", new SqlConnection(ConfigurationManager.AppSettings["StrConn"])); cmd.Connection.Open(); cmd2.Connection.Open(); SqlDataReader ddlValues; ddlValues = cmd.ExecuteReader(); SqlDataReader ddlValues1; ddlValues1 = cmd2.ExecuteReader(); ddlcity.DataSource = ddlValues; ddlcity.DataValueField = "City"; ddlcity.DataTextField = "City"; ddlcity.DataBind(); ddrCountry.DataSource = ddlValues1; ddrCountry.DataValueField = "Country"; ddrCountry.DataTextField = "Country"; ddrCountry.DataBind(); cmd2.Connection.Close(); cmd2.Connection.Dispose(); cmd.Connection.Close(); cmd.Connection.Dispose(); int nCount = 8; ddlcity.Items.Insert(nCount + 1, new ListItem("Other", "0")); } } protected void ddlcity_SelectedIndexChanged1(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["StrConn"]); conn.Open(); if (ddlcity.SelectedItem.Text == "Other") { txtOther.Visible = true; string insertquery1 = @"insert into City(Othercity) values('" + txtOther.Text + "')"; SqlCommand cmd3 = new SqlCommand(insertquery1, conn); cmd3.ExecuteNonQuery(); conn.Close(); } } protected void BtnSubmit_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["StrConn"]); conn.Open(); string insertquery = @"insert into Register(Name,Address1,Address2,Zipcode,City,Country,PhoneNo,Email,Sex) values('" + TxtName.Text + "','" + TxtAddr1.Text + "','" + TxtAddr2.Text + "','" + TxtZipcode.Text + "','" + ddlcity.SelectedItem + "','" + ddrCountry.SelectedItem + "','" + TxtPhoneno.Text + "','" + TxtEmail.Text + "','" + rdbSex.SelectedValue + "')"; SqlCommand cmd1 = new SqlCommand(insertquery, conn); cmd1.ExecuteNonQuery(); Label12.Text = "Record Registered!"; Label12.Visible = true; conn.Close(); }

Answers (1)