Jan 9, 2014

C# Load Data Into Data Grid On ComboBox Select Changed

private void COMBOBOXNAME_SelectedIndexChanged(object sender, EventArgs e)
{
      string Selecteditem = COMBOBOXNAME.Items[COMBOBOXNAME.SelectedIndex].ToString();
         
   string ConnectionString = @"Server=localhost;Database=dbname;Trusted_Connection=True";
 SqlConnection con = new SqlConnection(ConnectionString);
          try
            {
                con.Open();               
            }
          catch (SqlException ex)
            {
               
MessageBox.Show(ex.Message);
            }
         catch (Exception ex )
            {
                MessageBox.Show(ex.Message);
            }

            SqlCommand command = con.CreateCommand()

   command.CommandText = "SELECT * from Test";
         
   int iCount =0;

            SqlDataReader reader = command.ExecuteReader();
         
           DGName.Rows.Clear();
            while (reader.Read())
            {
                DGName.Rows.Add();
                DGName.Rows[iCount].Cells[0].Value = iCount + 1; // row number will be added
                DGName.Rows[iCount].Cells[1].Value = reader["DBColumnName"].ToString();
                DGName.[iCount].Cells[2].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[3].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[4].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[5].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[6].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[7].Value = reader["DBColumnName"].ToString();
                DGName.Rows[iCount].Cells[8].Value = reader["DBColumnName"].ToString();
             
                iCount++;
            }
            reader.Close();
           con.Close();

}

//DGName : Wnter Your Data Grid Name
//iCount : Counts The Rows
//COMBOBOXNAME : Enter Your ComboBoxName


No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...