string conString; //= @"Data Source = DESKTOP-TSV4T6E\MSSQL2014; Initial Catalog=Sample;User id=log1;Password=1"; protected void Page_Load(object sender, EventArgs e) { conString = WebClass.getConnectionStringByName("sqlSConString"); //Response.Write("conString: "+conString+"<br />"); if (!Page.IsPostBack) { #region Đọc dữ liệu và đưa lên danh sách dropdownlist //ddlTheLoai.Items.Clear(); ListItem item0 = new ListItem(); item0.Value = "-1"; item0.Text = "Chọn thể loại"; ddlTheLoai.Items.Add(item0); SqlConnection sqlcon = new SqlConnection(); SqlCommand sqlcom2 = new SqlCommand(); try { sqlcon.ConnectionString = conString; sqlcon.Open(); if (sqlcon.State == System.Data.ConnectionState.Open) { sqlcom2.Connection = sqlcon; sqlcom2.CommandType = System.Data.CommandType.Text; sqlcom2.CommandText = "SELECT maTheLoai, tenTheLoai FROM TheLoai"; SqlDataReader sqlreader = sqlcom2.ExecuteReader(); if (sqlreader.HasRows) { while (sqlreader.Read()) { ListItem item = new ListItem(); item.Value = sqlreader.GetInt32(0).ToString(); //ma the loai item.Text = sqlreader.GetSqlString(1).ToString(); //ten the loai ddlTheLoai.Items.Add(item); } } } } catch (Exception exc) { lThongBao.Text = String.Format("Lỗi: {0}. Chi tiết: {1}", exc.Message, exc.StackTrace); } finally { sqlcon.Close(); sqlcom2.Dispose(); } #endregion //đưa dữ liệu lên lưới dữ liệu gridview BindDataToGridView(-1); } }
protected void Page_Load(object sender, EventArgs e) { conString = WebClass.getConnectionStringByName("sqlSConString"); }