Monday, March 26, 2012

ok im stumped been trying to figure ou thow i can do this for a couple hours

ive got a drop down list that gets populated at the click of a button , it gets populated with the columns in any table name from my data base that i put into the text box... well my first thing i fixed that i thought would be a problem was if i hit the button twice itd populate the drop down twice and have duplicate veiwing ... so i added in a foreach to delete the code but the problem is it only runs through and deletes the column name from the table that id type into the box leaving the columns form the last table i typed in there.
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