|
-
February 23rd, 2012, 09:10 AM
#1
[RESOLVED] iteration from the database
Code:
private void rolePanel_Click(object sender, EventArgs e)
{
string DBConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\LinksApplication.mdf;Integrated Security=True;User Instance=True";
SqlConnection sqlConnection = new SqlConnection(DBConnection);
CheckBox roleCheck = new CheckBox();
//Panel rolePanel = new Panel();
try
{
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlCommand.CommandText = "SELECT * FROM Role";
/*SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet, "Role");
DataTable dataTable=dataSet.Tables["Role"];
foreach(DataRow dataRow in dataTable.Rows){
new CheckBox().Text=dataRow["role"].ToString();
rolePanel.Controls.Add(new CheckBox());
rolePanel.Visible = true;*/
SqlDataReader sqlDatareader = sqlCommand.ExecuteReader();
//CREATE AN INSTANCE OF A CHECKBOX AND SET ATTRIBUTES
roleCheck.TextAlign = ContentAlignment.MiddleRight;
while (sqlDatareader.Read())
{
roleCheck.Text = sqlDatareader["role"].ToString();
rolePanel.Controls.Add(roleCheck);
rolePanel.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sqlConnection.Close();
}
}
PLEASE KINDLY HELP ME OUT. I WANT TO ITERATE THE VALUES FROM THE DATABASE TABLE INTO A PANEL(rolePanel) AND ADDING CHECKBOX TO EACH ITEM, BUT FOR NOW IT ONLY POPULATES IT WITH JUST ONE ITEM FROM THE DATABASE TABLE. I THINK MY ITERATE STATEMENT ISN'T RIGHT, WHAT SHOULD I DO? THANKS. GOD BLESS IN JESUS NAME.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|