Showing posts with label base. Show all posts
Showing posts with label base. Show all posts

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

OK, answered my own question

My base class was implementing an abstract method,
forcing me to declare the whole class as abstract too.
VS.NET didn't like this for some reason, even though when
running the app, it appeared to work fine.

>--Original Message--
>I've got a couple of UserControls that are derived from
a
>common base-class. When I try to open them in the IDE,
>the following error is displayed in a dialog window:
>"The file failed to load in the Web Form designer.
Please
>correct the following error, then load it again: Type
>Abstract"
>This has only just started happening, as I've had the
>inheritance working for a good w or so now. I've no
>idea what I've changed to cause this.
>Please help!
>Ben
>.
>Yes this is the reason. I find this very annoying. The only solution is
to make the method virtual and to remove the abstract from the class.
Does anybody know of a better approach?
____________________
www.bloomfield.as.ro

Okay to override an ASP.NET page's constructor

See any problem with this code:

Here's my Base page class:

Public Class BasePage
Inherits System.Web.UI.Page

Protected m_NeedAlerts As Boolean

Private Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyBase.Init
Dim doSomethingWithThis As Boolean = m_NeedAlerts
End Sub

End Class

Now, in order to set m_NeedAlerts in a page that inherits from
BasePage, I need to do this, right?

Partial Class AddCertification
Inherits BasePage

Public Sub New()
MyBase.m_NeedAlerts = True
End Sub
End Class

Is there a problem with overriding the page's constructor? I've never
seen this done in all the code I've seen, so I am a little worried.
Is there a better way to do what I am trying to accomplichslolife,

There is no problem with overriding constructor, as long as you put
MyBase.New as the first statement in it. There are a few ways to achieve what
you need (set up m_NeedAlerts from inherited page):
1 - the one you specified - override constructor and set it there
2- override OnInit method in inherited class, set the variable, and then
call MyBase.OnInit
3 - declare a MustOverride method in the base class that will force setting
the variable and call it from your Page_Init sub of the base class before
examining its value

The advantage of 2 & 3 over 1 would be that you may need to refer to
controls' properties to determine the value of your variable, which are not
available during constructor.

HTH
"slolife" wrote:

Quote:

Originally Posted by

See any problem with this code:
>
Here's my Base page class:
>
Public Class BasePage
Inherits System.Web.UI.Page
>
Protected m_NeedAlerts As Boolean
>
Private Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyBase.Init
Dim doSomethingWithThis As Boolean = m_NeedAlerts
End Sub
>
End Class
>
Now, in order to set m_NeedAlerts in a page that inherits from
BasePage, I need to do this, right?
>
Partial Class AddCertification
Inherits BasePage
>
Public Sub New()
MyBase.m_NeedAlerts = True
End Sub
End Class
>
Is there a problem with overriding the page's constructor? I've never
seen this done in all the code I've seen, so I am a little worried.
Is there a better way to do what I am trying to accomplich
>
>


That's for the advice Sergey. I went with the constructor for now.

On May 3, 8:49 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@.discussions.microsoft.comwrote :

Quote:

Originally Posted by

slolife,
>
There is no problem with overriding constructor, as long as you put
MyBase.New as the first statement in it. There are a few ways to achieve what
you need (set up m_NeedAlerts from inherited page):
1 - the one you specified - override constructor and set it there
2- override OnInit method in inherited class, set the variable, and then
call MyBase.OnInit
3 - declare a MustOverride method in the base class that will force setting
the variable and call it from your Page_Init sub of the base class before
examining its value
>
The advantage of 2 & 3 over 1 would be that you may need to refer to
controls' properties to determine the value of your variable, which are not
available during constructor.
>
HTH
>
"slolife" wrote:

Quote:

Originally Posted by

See any problem with this code:


>

Quote:

Originally Posted by

Here's my Base page class:


>

Quote:

Originally Posted by

Public Class BasePage
Inherits System.Web.UI.Page


>

Quote:

Originally Posted by

Protected m_NeedAlerts As Boolean


>

Quote:

Originally Posted by

Private Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyBase.Init
Dim doSomethingWithThis As Boolean = m_NeedAlerts
End Sub


>

Quote:

Originally Posted by

End Class


>

Quote:

Originally Posted by

Now, in order to set m_NeedAlerts in a page that inherits from
BasePage, I need to do this, right?


>

Quote:

Originally Posted by

Partial Class AddCertification
Inherits BasePage


>

Quote:

Originally Posted by

Public Sub New()
MyBase.m_NeedAlerts = True
End Sub
End Class


>

Quote:

Originally Posted by

Is there a problem with overriding the page's constructor? I've never
seen this done in all the code I've seen, so I am a little worried.
Is there a better way to do what I am trying to accomplich

Okay to override an ASP.NET page's constructor

See any problem with this code:
Here's my Base page class:
Public Class BasePage
Inherits System.Web.UI.Page
Protected m_NeedAlerts As Boolean
Private Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles MyBase.Init
Dim doSomethingWithThis As Boolean = m_NeedAlerts
End Sub
End Class
Now, in order to set m_NeedAlerts in a page that inherits from
BasePage, I need to do this, right?
Partial Class AddCertification
Inherits BasePage
Public Sub New()
MyBase.m_NeedAlerts = True
End Sub
End Class
Is there a problem with overriding the page's constructor? I've never
seen this done in all the code I've seen, so I am a little worried.
Is there a better way to do what I am trying to accomplichslolife,
There is no problem with overriding constructor, as long as you put
MyBase.New as the first statement in it. There are a few ways to achieve wha
t
you need (set up m_NeedAlerts from inherited page):
1 - the one you specified - override constructor and set it there
2- override OnInit method in inherited class, set the variable, and then
call MyBase.OnInit
3 - declare a MustOverride method in the base class that will force setting
the variable and call it from your Page_Init sub of the base class before
examining its value
The advantage of 2 & 3 over 1 would be that you may need to refer to
controls' properties to determine the value of your variable, which are not
available during constructor.
HTH
"slolife" wrote:

> See any problem with this code:
> Here's my Base page class:
> Public Class BasePage
> Inherits System.Web.UI.Page
> Protected m_NeedAlerts As Boolean
> Private Sub Page_Init(ByVal sender As Object, ByVal e As
> EventArgs) Handles MyBase.Init
> Dim doSomethingWithThis As Boolean = m_NeedAlerts
> End Sub
> End Class
> Now, in order to set m_NeedAlerts in a page that inherits from
> BasePage, I need to do this, right?
> Partial Class AddCertification
> Inherits BasePage
> Public Sub New()
> MyBase.m_NeedAlerts = True
> End Sub
> End Class
> Is there a problem with overriding the page's constructor? I've never
> seen this done in all the code I've seen, so I am a little worried.
> Is there a better way to do what I am trying to accomplich
>
That's for the advice Sergey. I went with the constructor for now.
On May 3, 8:49 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@.discussions.microsoft.com> wrote:
> slolife,
> There is no problem with overriding constructor, as long as you put
> MyBase.New as the first statement in it. There are a few ways to achieve w
hat
> you need (set up m_NeedAlerts from inherited page):
> 1 - the one you specified - override constructor and set it there
> 2- override OnInit method in inherited class, set the variable, and then
> call MyBase.OnInit
> 3 - declare a MustOverride method in the base class that will force settin
g
> the variable and call it from your Page_Init sub of the base class before
> examining its value
> The advantage of 2 & 3 over 1 would be that you may need to refer to
> controls' properties to determine the value of your variable, which are no
t
> available during constructor.
> HTH
> "slolife" wrote:
>
>
>
>
>
>
>
>
>