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