Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yamoo
Starting Member
11 Posts |
Posted - 2013-08-07 : 11:09:26
|
public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(); SqlCommand com = new SqlCommand(); SqlDataAdapter dap = new SqlDataAdapter(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataTable dt1 = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { load(); } private void load() { ds.Tables.Clear(); dt.Rows.Clear(); con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; //go to connection string to identify connection com.CommandText = "select employeeid,firstname,lastname from employees order by employeeid"; //write command com.Connection = con; dap.SelectCommand = com; dt.TableName = "employees"; ds.Tables.Add(dt); dap.Fill(ds.Tables["employees"]); for (int i = 0; i < dt.Rows.Count; i++) { cobedi.Items.Add(dt.Rows[i][0].ToString()); cobdel.Items.Add(dt.Rows[i][0].ToString()); } } protected void btnview_Click(object sender, EventArgs e) { view(); } private void view() { ds.Tables.Clear(); dt.Rows.Clear(); con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "select employeeid,FirstName,LastName from employees order by employeeid"; com.Connection = con; dap.SelectCommand = com; dt.TableName = "employees"; ds.Tables.Add(dt); dap.Fill(ds.Tables["employees"]); GridView1.DataSource = dt; GridView1.DataBind(); } protected void viewname_Click(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "select FirstName from employees where employeeid=@e"; com.Parameters.Add("@e", SqlDbType.NVarChar, 30).Value = txtenter.Text; com.Connection = con; dap.SelectCommand = com; dap.Fill(dt1); txtname.Text = dt1.Rows[0][0].ToString(); } protected void btnadd_Click(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "insert into employees (FirstName,LastName) values (@f,@l)"; com.Parameters.Add("@f", SqlDbType.NVarChar, 30).Value = txtfst.Text; com.Parameters.Add("@l", SqlDbType.NVarChar, 30).Value = txtlst.Text; com.Connection = con; dap.InsertCommand = com; dr = ds.Tables["employees"].NewRow(); dr["FirstName"] = txtfst.Text; dr["LastName"] = txtlst.Text; ds.Tables["employees"].Rows.Add(dr); dap.Update(ds.Tables["employees"]); view(); load(); } protected void btnupdate_Click(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "update employees set firstname=@f1 where employeeid=@id"; com.Parameters.Add("@f1", SqlDbType.NVarChar, 30).Value = txtnname.Text; com.Parameters.Add("@id", SqlDbType.Int).Value = cobedi.SelectedItem.ToString(); com.Connection = con; dap.UpdateCommand = com; dr = ds.Tables["employees"].Rows[cobedi.SelectedIndex]; dr["FirstName"] = txtnname.Text; dap.Update(ds.Tables["employees"]); view(); } protected void btndel_Click(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; com.CommandText = "delete from employees where employeeid=@id1"; com.Parameters.Add("@id1", SqlDbType.Int).Value = cobdel.SelectedItem.ToString(); com.Connection = con; dap.DeleteCommand = com; dr = ds.Tables["employees"].Rows[cobdel.SelectedIndex]; dr.Delete(); dap.Update(ds.Tables["employees"]); view(); load(); } values are repeat in dropdownlists what is solution ?when I add cobedi.Items.Clear(); cobdel.Items.Clear(); above for loop in page_load ...... UPDATE and DELETE button not work . |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-07 : 11:34:02
|
I am not able to discern from your posting which of your queries against the database fills the dropdown list. Identify which query fills that database and look at that query (or post the query) to see why it is sending you duplicates. Then, depending on the reason, you may need to include a DISTINCT clause in your SELECT statement or do something else to avoid duplicates.Another possibility is that you may not be clearing the .Net data tables between successive calls to the database. Again, I am not able to see if that is the problem from the code you have posted. |
 |
|
yamoo
Starting Member
11 Posts |
Posted - 2013-08-07 : 11:56:15
|
protected void Page_Load(object sender, EventArgs e){load();}private void load(){ds.Tables.Clear();dt.Rows.Clear();con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; //go to connection string to identify connectioncom.CommandText = "select employeeid,firstname,lastname from employees order by employeeid"; //write commandcom.Connection = con;dap.SelectCommand = com;dt.TableName = "employees";ds.Tables.Add(dt);dap.Fill(ds.Tables["employees"]);for (int i = 0; i < dt.Rows.Count; i++){cobedi.Items.Add(dt.Rows[i][0].ToString());cobdel.Items.Add(dt.Rows[i][0].ToString());}}I want to load "employeeie" column in dropdownlist without duplicate . and this code is complete |
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-07 : 12:58:06
|
quote: Originally posted by yamoo protected void Page_Load(object sender, EventArgs e){load();}private void load(){ds.Tables.Clear();dt.Rows.Clear();con.ConnectionString = ConfigurationManager.ConnectionStrings["north"].ConnectionString; //go to connection string to identify connectioncom.CommandText = "select employeeid,firstname,lastname from employees order by employeeid"; //write commandcom.Connection = con;dap.SelectCommand = com;dt.TableName = "employees";ds.Tables.Add(dt);dap.Fill(ds.Tables["employees"]);for (int i = 0; i < dt.Rows.Count; i++){cobedi.Items.Add(dt.Rows[i][0].ToString());cobdel.Items.Add(dt.Rows[i][0].ToString());}}I want to load "employeeie" column in dropdownlist without duplicate . and this code is complete
That seems fine to me. So the problem could be due to one of these:a) there are duplicates in the employees table. Check in the database table to see if that is the case. If employeeid is the primary key this should not happen.b) Somehow you are filling the dropdown list twice. Search for all instances of cobdel and put breakpoints there to see whether you are invokding that code twice. |
 |
|
yamoo
Starting Member
11 Posts |
Posted - 2013-08-07 : 15:37:46
|
no all steps is true , but the duplicate happen when i add or delete or update row from table . |
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-07 : 16:35:39
|
You should put a statement just outside the for loop in load function and see if it gets called each time you delete or update. If it does, then that explains why you are getting the duplicates. If that indeed is the problem, you should examine the logic - i.e., is that the right place to fill the data for that drop down list? |
 |
|
|
|
|
|
|