Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL
i have a form which contains one textbox txtDistrict and a datagridview with one column clmTown
 
i want to display data from the db to the fixed column. the code i am using works but creates additional column to the exsisting one. how do i go about this
 
code
 
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;
        }
 

itry using this but still does not works. please help me out
 
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();
            }
	}
Posted 20-Apr-13 21:39pm
Edited 20-Apr-13 22:21pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should customize your GridView in the ASP page by settings by letting there only one column (remove the others) like in the next example:
 
<asp:BoundField DataField="Town" HeaderText='Town'
                    SortExpression="Town" />
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100