Hi,

I am using c# to update and view the student_Teacher simple database in sql. I used textboxes with binding navigator to view and update the teachers info and datagridview for the students, so 1 teacher per many students is the output. I have no problem for searching, editing and delete coz It is working properly, but my only problem is that adding a new record to the database. Actually, one of my solution for this is to use separate save button for textboxes and datagridview but it causes to a delayed and more hassle environment. So I just want it to have only one save button in a form that saves all the fields at the database. Is it possible to do this? Anyway, here is the list of code as shown:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace TeachSudent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this .taTeachers.Fill(this .dsTeachers.Teachers);
                this .taStudents.Fill(this .dsStudent.Students, int .Parse(txtId.Text.ToString()));
            }
            catch (FormatException )
            {}
        }
    private void teachersBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
            this .Validate();
            this .bsTeachers.EndEdit();
            this .tamTeacher.UpdateAll(this .dsTeachers);

            //TeacherId is the foreign key to Student
            //txtId.text is the input indicator for TeacherId at Student table,
            //so setting the property of txtTeacherId.text, Databindings: Text: bsStudents – TeacherId
             //Then the property for txtId.text, Databindings: Text: bsTeachers - ID

            txtTeacherId.Text = txtId.Text;
            this .Validate();
            this .bsStudents.EndEdit();
            this .tamStudents.UpdateAll(this .dsStudent);
        }

        private void studentsDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //txtTeacherId.Text = txtId.Text;
        }
        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            //txtTeacherId.Text = txtId.Text;
        }
    }
}

Then here is my query at dsStudent, StudentsTableAdapter:

For Fill(@id):

SELECT     ID, TeacherId, Name, BDay, Address, Absences
FROM         Students
WHERE     (TeacherId = @id)

For DeleteQuery(@Original_ID):

DELETE FROM Students
WHERE     (ID = @Original_ID)

For UpdateQuery(@TeacherId, @Name…):

UPDATE    Students

SET  TeacherId = @TeacherId, Name = @Name, BDay = @BDay, Address = @Address, Absences = @Absences
WHERE     (ID = @Original_ID)

And the query for dsTeachers, TeachersTableAdapter:

For Fill,GetData():

SELECT ID, Name, Bithday, Address FROM dbo.Teachers

Actually, the problem for this is that when you perform Add new data entry for the Teachers and datagridview for the list of Students, it saves the other fields but TeachersId produces a null value except the last line of the grid, so when performing loading of forms only one row that has the teachersId had been shown.

So I need help to solve this problem, your help will be greatly appreciated.

Thanks and God Bless.

Har

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Hi,

For clearer explanation for this I will provide an example for adding an entry to teachers and student table below, coz im not too good in explanation.ü

Ex:

@Textboxes for teachers info:

TeacherID: 5, autoIncremented
Name: Linda
Bday: Jan. 25
Address:Main Rd.

@Datagridview for Students info:

Name Bday Address Absences

Ed Feb. 13 113 Vill 4
Rey Dec. 25 #56 1
Chad Oct 3 #12 0

So When i save this data using the code above produces an output at the database as shown:

For Teachers database:

TeacherID Name Bday Address

5 Linda Jan. 25 Main Rd.

For Students Database:

ID TeacherId Name Bday Address Absences

1 Null Ed Feb. 13 113 Vill 4
2 Null Rey Dec. 25 #56 1
3 5 Chad Oct 3 #12 0

From this output Ed and Rey have a null TeachersId value while only chad has a value, so I want rey and Ed to have a teachersId value, that is my problem using only one save button in my application, I need help for this problem coz i tried everything but nothing happens.

Tnx in advance.

Har

Anyway, TeacherId is autoIncremented so if you press add at the navigator, its value is -1. From that sample i assume the value that is generated is 5.

Har

Hi,

Anyway, i created a solution that solves for my problem in master/detail relationship, but not really the best one. so here is the revised code as shown below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MasterChild
{
    public partial class Form1 : Form
    {
        bool addIndicator = false;
        DataSet ds = new DataSet();
        int lastNoOfMasterId;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.masterTableAdapter.Fill(this.dsMaster.Master);
            detailLoadValue();
            lastno();
        }
        void lastno()
        {
            bnMoveLastItem.PerformClick();
            lastNoOfMasterId = int.Parse(txtMasterId.Text);
        }
        void detailLoad()
        {
            try
            {
                this.detailTableAdapter.Fill(this.dsDetail.Detail, new System.Nullable<int>(((int)(System.Convert.ChangeType(lblMasterId.Text, typeof(int))))));
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
        void detailLoadValue()
        {
            lblMasterId.Text = txtMasterId.Text;
            detailLoad();
        }
        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            detailLoadValue();
        }
        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            detailLoadValue();
        }
        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            detailLoadValue();
        }
        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            detailLoadValue();
        }
        private void masterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            ds.EnforceConstraints = false;
            this.masterBindingSource.EndEdit();
            this.masterTableAdapter.Update(this.dsMaster.Master);

            this.detailBindingSource.EndEdit();
            this.detailTableAdapter.Update(this.dsDetail.Detail);
            ds.EnforceConstraints = true;
            addIndicator = false;
        }
        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            if (addIndicator)
            {
                return;
            }
            else
            {
                detailLoadValue();
                lastNoOfMasterId += 1;
                addIndicator = true;
            }          
        }
        private void dgvDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (addIndicator)
            {            
                lblMasterId.Text = lastNoOfMasterId.ToString();
            }
            else
            {
                lblMasterId.Text = txtMasterId.Text;
            }
        }
    }
}

So using the integer lastNoOfMasterId that determines the last number of MasterId and automatically incremented if the user press an add button, which is now a value of MasterId at Details datagrid, replacing the -1 default value during add. With this i am not having trouble now on null value of MasterId at details table and adding a new master along with its details is no longer a problem to me.

But i think this kind of code is much more complicated. Is there any simplified one aside from this?

tnx.

Har..

Edited 6 Years Ago by __avd: Added [code] tags.

This question has already been answered. Start a new discussion instead.