jude_2 0 Newbie Poster

i have a logical error in my code where by i will read data into my four textboxes from sql database and the textboxes will be filled but when i click on a textbox the data will appear in the metrogrid(from metro framework) but when clicking another textbox the data in the other textboxes except the one that i first clicked will disappear. i used dataset i have the same problem and also datareader, i had the same problem. please i need help.
Here is the code:

   private void btnSearch_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection(@"server = .\sql2012; database = dbAcceptance; integrated security = true; "))
        {
            string sql1 = $"select * from acceptance WHERE regno = {txtregno.Text}";
            connection.Open();

            try
            {
                // Create Data Adapter
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(sql1, connection);

                // Create and Fill Data Set 
                DataSet ds = new DataSet();
                da.Fill(ds, "acceptance");

                // Get the data tables collection 
                DataTableCollection dtc = ds.Tables;
                foreach (DataRow row in dtc["acceptance"].Rows)
                {
                    txtfullname.AppendText(row["fullname"].ToString());
                    txtdepartment.AppendText(row["Department"].ToString());
                    txtfaculty.AppendText(row["faculty"].ToString());
                    txtmodeofstudy.AppendText(row["modeofstudy"].ToString());
                    txtprogramme.AppendText(row["programmeofstudy"].ToString());
                }

            }
            catch (Exception ex)
            {
                MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                connection.Close();
            }
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        objState = EntityState.Added;
        pContainer.Enabled = true;
        registeredBindingSource.Add(new Registered());
        registeredBindingSource.MoveLast();
        txtregno.Focus();
    }

    void ClearInput()
    {
        txtregno.Text = null;
        txtstudentid.Text = null;
        txtprogramme.Text = null;
        txtmodeofstudy.Text = null;
        txtfullname.Text = null;
        txtfaculty.Text = null;
        txtdepartment.Text = null;
        cmbcoursestatus.Text = null;
        cmbcoursetitle.Text = null;
        cmbcourseunit.Text = null;
        cmbcurrentsemester.Text = null;
        cmbfinalsemester.Text = null;
        cmblevel.Text = null;
    }

    private void CourseRegistration_Load(object sender, EventArgs e)
    {
        try
        {
            using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
            {
                if (db.State == ConnectionState.Closed)
                    db.Open();
                registeredBindingSource.DataSource = db.Query<Registered>("select * from RegisteredCourses", commandType: CommandType.Text);
                pContainer.Enabled = false;
            }
        }
        catch (Exception ex)
        {

            MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        pContainer.Enabled = false;
        registeredBindingSource.ResetBindings(false);
        //ClearInput();
        this.CourseRegistration_Load(sender, e);
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        objState = EntityState.Changed;
        pContainer.Enabled = true;
        txtregno.Focus();
    }

    private void gridContainer_CellClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        objState = EntityState.Deleted;
        if (MetroFramework.MetroMessageBox.Show(this,"Are You Sure You Want To Delete This Record ?","Message",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
        {
            try
            {
                Registered obj = registeredBindingSource.Current as Registered;
                if (obj != null)
                {
                    using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
                    {
                        if (db.State == ConnectionState.Closed)
                            db.Open();
                        int result = db.Execute("delete from RegisteredCourses where StudentID = @StudentID", new { StudentID = obj.StudentID }, commandType: CommandType.Text);
                        if (result != 0)
                        {
                            registeredBindingSource.RemoveCurrent();
                            pContainer.Enabled = false;
                            objState = EntityState.unchanged;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MetroFramework.MetroMessageBox.Show(this, ex.Message,"Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            registeredBindingSource.EndEdit();
            Registered obj = registeredBindingSource.Current as Registered;
            if (obj != null)
            {
                using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
                {
                    if (db.State == ConnectionState.Closed)
                        db.Open();
                    if (objState == EntityState.Added)
                    {
                        DynamicParameters p = new DynamicParameters();
                        p.Add("@StudentID", dbType: DbType.Int32, direction: ParameterDirection.Output);
                        p.AddDynamicParams(new { FullName = obj.FullName, RegNo = obj.RegNo, CourseTitle = obj.CourseTitle, CourseStatus = obj.CourseStatus, CourseUnit = obj.CourseUnit, Department = obj.Department, CurrentSemester = obj.CurrentSemester, CurrentSession = obj.CurrentSession, ModeOfStudy = obj.ModeOfStudy, Level = obj.Level, ProgrammeOfStudy = obj.ProgrammeOfStudy, Faculty = obj.Faculty, finalSemester = obj.finalSemester});
                        db.Execute("sp_StudentRegisteredCourse_insert", p, commandType: CommandType.StoredProcedure);
                        obj.StudentID = p.Get<int>("@StudentID");
                    }
                    else
                    {
                        db.Execute("sp_StudentRegisteredCourse_update", new { StudentID = obj.StudentID, FullName = obj.FullName, RegNo = obj.RegNo, CourseTitle = obj.CourseTitle, CourseStatus = obj.CourseStatus, CourseUnit = obj.CourseUnit, Department = obj.Department, CurrentSemester = obj.CurrentSemester, CurrentSession = obj.CurrentSession, ModeOfStudy = obj.ModeOfStudy, Level = obj.Level, ProgrammeOfStudy = obj.ProgrammeOfStudy, Faculty = obj.Faculty, finalSemester = obj.finalSemester },commandType:CommandType.StoredProcedure);
                    }
                    gridContainer.Refresh();
                    pContainer.Enabled = false;
                    objState = EntityState.unchanged;
                }
            }
        }
        catch (Exception ex)
        {
            MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.