Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I need help with this problem:

I have Items table in sql and Group table!

What i need:

on load event of form i want to show Buttons to number of rows in Group Table and Buttons must have text of grup name!

After the buttons are displayed i want to show items on buttons and when i click a item button , the event of buttons must be Click. After clicking button listbox must be filled with data from items table!


P.S .Sorry for my english!....
Posted

1 solution

This should get you started
using(SqlConnection conn = new SqlConnection(...))
{
  using(SqlCommand cmd = new SqlCommand("SELECT ID, Name FROM Group", conn))
  {
    DataReader dr = cmd.ExecuteReader();
    while(dr.Read())
    {
       Button btn = new Button();
       btn.Text = dr["Name"].ToString();
       btn.CommandArgument = dr["ID"].ToString();
       btn.Command += new CommandEventHandler(OnBtnCommand);
       Controls.Add(btn);
    }
  }
}

protected void OnBtnCommand(object sender, CommandEventArgs e)
{
   // Load dropdown based on 
   int ID = int.Parse(e.CommandArgument);
}
 
Share this answer
 
Comments
Atmir 13-Apr-11 17:11pm    
its ok but i need in VB.NET not in C#!
Dave Kreskowiak 13-Apr-11 17:20pm    
So? You'd be amazed at how similar the two languages are if you just bothered to look at side-by-side examples posted all over MSDN.

Using conn As SqlConnection = New SqlConnection(...)

Using cmd As SqlCommand = New SqlCommand("SELECT ID, Name FROM Group", conn)

Dim dr As DataReader = cmd.ExecuteReader() Do While dr.Read()

Dim btn As Button = New Button()

btn.Text = dr("Name").ToString()

... You don't say if you're using Windows Forms or ASP.NET (Web App)...

Controls.Add(btn)
Loop
End Using
End Using
[no name] 13-Apr-11 17:54pm    
Learn it.

If you plan on doing .NET development for a while you should learn to read both
Atmir 15-Apr-11 4:48am    
how to use this " btn.Command += new CommandEventHandler(OnBtnCommand);"in vb.net?

i use
"class Public Class CommandEventArgs
Inherits EventArgs

End Class"
but is not working? any idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900