heres the code
private void Button2_Click(object sender, System.EventArgs e)
{
SqlConnection rate;
SqlDataAdapter Selct;
string db;
rate = new SqlConnection( @dotnet.itags.org."xoxo");
rate.Open();
db = TextBox5.Text;
Selct = new SqlDataAdapter( "Select * From " + db, rate );
DataSet ds = new DataSet();
Selct.Fill(ds, db);
foreach (DataColumn dsc in ds.Tables[0].Columns)
{
DropDownList2.Items.Remove(dsc.ColumnName);
DropDownList2a.Items.Remove(dsc.ColumnName);
DropDownList2b.Items.Remove(dsc.ColumnName);
DropDownList2c.Items.Remove(dsc.ColumnName);
DropDownList2d.Items.Remove(dsc.ColumnName);
}
foreach (DataColumn dsc in ds.Tables[0].Columns)
{
DropDownList2.Items.Add(dsc.ColumnName);
DropDownList2a.Items.Add(dsc.ColumnName);
DropDownList2b.Items.Add(dsc.ColumnName);
DropDownList2c.Items.Add(dsc.ColumnName);
DropDownList2d.Items.Add(dsc.ColumnName);
}
}Try to use DataBind() method instead of looping.
And, you can remove the data in DropDownList with setting it null.
Try below
SqlConnection con = new SqlConnection("connectionstring");
DataTable dt = new DataTable();
string query ="select [TextString],[ValueString] from [table]";
SqlDataAdapter da = new SqlDataAdapter(query,con);
//set column names
ddlSample.DataTextField = "TextString"; //will be shown in the list
ddlSample.DataValueField = "ValueString";
// won't be shown but value for each item of the list
// DropDownList has Key/Value pair data structure.
con.open();
da.Fill(dt);
con.close();
ddlSample.DataSource = dt.DefaultView;
ddlSample.DataBind();
// I think that if dropdownlist got databound, the dropdownlist automatically reset.
If you want to make sure to reset the dropdownlist,
ddlSample = new DropDownList();
before databinding.
sorry im having trouble getting this to work and understanding how this would work
it took me a lot of trial and error plus thinking to make use of what i already had and i came up with using an invisible textbox lol like this
private void Button2_Click(object sender, System.EventArgs e)
{
SqlConnection Con = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["PubsString"]);
Con.Open();
string TxtBx = TextBox5.Text;
string TxtBx1 = Dupe.Text;
SqlDataAdapter DtAdapt = new SqlDataAdapter( "Select * From " + TxtBx, Con );
DataSet ds = new DataSet();
DtAdapt.Fill(ds,TxtBx);
foreach (DataColumn dsc in ds.Tables[0].Columns)
{
DropDownList2.Items.Add(dsc.ColumnName);
DropDownList2a.Items.Add(dsc.ColumnName);
DropDownList2b.Items.Add(dsc.ColumnName);
DropDownList2c.Items.Add(dsc.ColumnName);
DropDownList2d.Items.Add(dsc.ColumnName);
}
if (TxtBx1 != " ")
{
SqlDataAdapter DtAdapt1 = new SqlDataAdapter( "Select * From " + TxtBx1, Con );
DtAdapt1.Fill(ds,TxtBx1);
foreach (DataColumn dsc in ds.Tables[0].Columns)
{
DropDownList2.Items.Remove(dsc.ColumnName);
DropDownList2a.Items.Remove(dsc.ColumnName);
DropDownList2b.Items.Remove(dsc.ColumnName);
DropDownList2c.Items.Remove(dsc.ColumnName);
DropDownList2d.Items.Remove(dsc.ColumnName);
}
}
Dupe.Text = TxtBx;
}
Feel free to critisize and help me find any other ways of doing this...
i suppose i could learn to close my connection
I could be wrong but isn't there a .Clear() method for you to use instead of looping over the items and removing them?
ya it works i just have to adjust a few things to get my application functional with that...
but righ tnow im just trying to figure out the difference between my program before i saved it and closed vs.net and now that ive opened it because now its not running right... i noticed it changed some events that were associated with button clicks... but still at that it needs something else
0 comments:
Post a Comment