load dataGrid C#
C#
5
Posts
3
Posters
0
Views
1
Watching
-
I would like to fill my datagrid from sql database in accordance with the principles of the n-tier architecture (DAL,BLL,UI), but I do not know how to do it in connected mode
Not quiet certain on the above 'principles' but try this:
try { string query = @"SELECT \* FROM \[contacts\]"; conn.Close();//you must have created this connection string already SqlCommand cmd = conn.CreateCommand();//sql command cmd.CommandText = query;//declared above conn.Open();//open your connection to the database SqlDataReader reader = cmd.ExecuteReader();//execute reader to read from database DataSet dataset = new DataSet();//you need this DataTable table = dataset.Tables.Add(); table.Columns.Add("contactid", typeof(string));//these table.Columns.Add("firstname", typeof(string));//must be column table.Columns.Add("lastname", typeof(string));//names in table.Columns.Add("email", typeof(string));//your datagrid while (reader.Read()) { //add rows to your table which already has column names/heards...see above table.Rows.Add(reader\["contact\_id"\].ToString(), reader\["name"\].ToString(), reader\["surname"\].ToString(), reader\["email"\].ToString()); } //check if any data was added in a while(reader.Read()) if (dataset.Tables\[0\].Rows.Count > 0 || dataset.Tables.Count > -1) { contacts.DataSource = table;//just like it says contacts.DataBind(); contacts.Visible = true; } } catch (SqlException fc) { error.Text = "Error occured :" + fc.Message;//catch that error } finally { conn.Close();//please close connection to your database when done } //hope this helps!!!
Later
-
Not quiet certain on the above 'principles' but try this:
try { string query = @"SELECT \* FROM \[contacts\]"; conn.Close();//you must have created this connection string already SqlCommand cmd = conn.CreateCommand();//sql command cmd.CommandText = query;//declared above conn.Open();//open your connection to the database SqlDataReader reader = cmd.ExecuteReader();//execute reader to read from database DataSet dataset = new DataSet();//you need this DataTable table = dataset.Tables.Add(); table.Columns.Add("contactid", typeof(string));//these table.Columns.Add("firstname", typeof(string));//must be column table.Columns.Add("lastname", typeof(string));//names in table.Columns.Add("email", typeof(string));//your datagrid while (reader.Read()) { //add rows to your table which already has column names/heards...see above table.Rows.Add(reader\["contact\_id"\].ToString(), reader\["name"\].ToString(), reader\["surname"\].ToString(), reader\["email"\].ToString()); } //check if any data was added in a while(reader.Read()) if (dataset.Tables\[0\].Rows.Count > 0 || dataset.Tables.Count > -1) { contacts.DataSource = table;//just like it says contacts.DataBind(); contacts.Visible = true; } } catch (SqlException fc) { error.Text = "Error occured :" + fc.Message;//catch that error } finally { conn.Close();//please close connection to your database when done } //hope this helps!!!
Later