public void studentmoney() { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); string strConn = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike"; string strSQL = "select Town from districtnt where District = '" + txtDistrict.Text + "'"; //conn.Open(); SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConn); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); // Populate a new data table and bind it to the BindingSource. DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapter.Fill(table); bindingSource1.DataSource = table; // you can make it grid readonly. dgvDistrict.ReadOnly = true; // finally bind the data to the grid dgvDistrict.DataSource = bindingSource1; }
private void txtDistrict_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); conn.ConnectionString = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike"; string sqlQuery = null; sqlQuery = "select * from DistrictnT where District = '" + txtDistrict.Text + "'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = sqlQuery; cmd.CommandType = System.Data.CommandType.Text; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); try { while (dr.Read()) { dgvDistrict.Rows[dgvDistrict.Rows.Count - 1].Cells["clmTowns"].Value = dr[2].ToString(); } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { conn.Close(); } }
<asp:BoundField DataField="Town" HeaderText='Town' SortExpression="Town" />
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)