Click here to Skip to main content
15,889,462 members
Home / Discussions / C#
   

C#

 
GeneralRe: Export SQL Server Data to Excel Using C#? Pin
OldZero16-Mar-15 22:17
OldZero16-Mar-15 22:17 
GeneralRe: Export SQL Server Data to Excel Using C#? Pin
Pete O'Hanlon16-Mar-15 22:22
mvePete O'Hanlon16-Mar-15 22:22 
GeneralRe: Export SQL Server Data to Excel Using C#? Pin
OldZero16-Mar-15 22:43
OldZero16-Mar-15 22:43 
AnswerRe: Export SQL Server Data to Excel Using C#? Pin
Gerry Schmitz16-Mar-15 14:02
mveGerry Schmitz16-Mar-15 14:02 
Questionhow to open and read .po(translation) file Pin
Member 1140010015-Mar-15 8:07
Member 1140010015-Mar-15 8:07 
AnswerRe: how to open and read .po file Pin
Dave Kreskowiak15-Mar-15 10:01
mveDave Kreskowiak15-Mar-15 10:01 
AnswerRe: how to open and read .po(translation) file Pin
Brisingr Aerowing26-Mar-15 2:40
professionalBrisingr Aerowing26-Mar-15 2:40 
QuestionHow to edit a datagridview/textboxes using tableadapters in c# and sql Pin
Member 1152545314-Mar-15 15:37
Member 1152545314-Mar-15 15:37 
I am wondering what is the best way to edit a database that has multiple tables.

(Section 1) I have a Administration, Teacher and Student table that is linked up with the Person table which has a Primary Key.

The Person table consists of the general information of the person.

The Student table consists of information about the student; student ID and qualification code.

The Teacher table consists of information about the teacher; teacher ID, Reg No and password.

The Administration table consists of information about the admin; admin ID, Role and password.

As the Primary Key in the Person table is the ID, I have linked up with each of the other's table with their appropriate ID.

(Section 2) I have a Course and Qualification table that is linked up with the Student and Teacher table as well as each other.

The course table constists of the Course ID, Course Name ... and Teacher ID.

The Qualifications table consists of Qualification Code, Qualification Name and Duration.

There is a section where I have to create a view which shows just the Student ID, Course ID and has the Student's Marks in it.

I have got a combobox which then links up with the dgv(datagridview).

I have got insert and delete methods for both sections. Here is an example of the insert method into the Admin/Person table.

C#
try
 {
personTableAdapter.Insert(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);

administrationTableAdapter.Insert(Convert.ToInt32(aAID1.Text), aRole.Text, aPassword.Text);
MessageBox.Show(aFirstName.Text + " " + aSurname.Text + " has been added. Your ID is " + aAID1.Text);

this.personTableAdapter.Fill(this._30002195DataSet.Person);

this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Here is an example of the delete method in the Admin/Person table.

C#
try
            {
                personTableAdapter.Delete(Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
                administrationTableAdapter.Delete(Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);
                MessageBox.Show("Person Deleted");
                this.personTableAdapter.Fill(this._30002195DataSet.Person);
                this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);

            }
            catch (Exception)
            {
                MessageBox.Show("Cannot delete Person");
            }


Those methods above are working fine, what I'm having problems with, is with the editing/updating part.

I have tried a few things and haven't worked.

Here's an example of an edit, while trying to use textboxes.

SQL
personTableAdapter.Update(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text, Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
                administrationTableAdapter.Update(aRole.Text, aPassword.Text, Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);
                

                personBindingSource.EndEdit();
                administrationBindingSource.EndEdit();
                administrationTableAdapter.Update(_30002195DataSet.Administration);
                personTableAdapter.Update(_30002195DataSet.Person);
                MessageBox.Show("Person Updated");

                this.personTableAdapter.Fill(this._30002195DataSet.Person);
                this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);



Here I tried to do the new values/original values, while trying to use textboxes

SQL
personTableAdapter.Update(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text, Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);

administrationTableAdapter.Update(aRole.Text, aPassword.Text, Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);


Trying to use the example through the mdsn, this is trying to use the datagridview.

this.Validate();
                personBindingSource.EndEdit();
                teacherBindingSource.EndEdit();

                _30002195DataSet.PersonDataTable deletedPerson = (_30002195DataSet.PersonDataTable)
                    _30002195DataSet.Person.GetChanges(DataRowState.Deleted);

                _30002195DataSet.PersonDataTable newPerson = (_30002195DataSet.PersonDataTable)
                    _30002195DataSet.Person.GetChanges(DataRowState.Added);

                _30002195DataSet.PersonDataTable modifiedPerson = (_30002195DataSet.PersonDataTable)
                _30002195DataSet.Person.GetChanges(DataRowState.Modified);

                try
                {
                    if (deletedPerson != null)
                    {
                        personTableAdapter.Update(deletedPerson);
                    }

                    teacherTableAdapter.Update(_30002195DataSet.Teacher);

                    if (newPerson != null)
                    {
                        personTableAdapter.Update(newPerson);
                    }

                    if (modifiedPerson != null)
                    {
                        personTableAdapter.Update(modifiedPerson);
                    }
                    _30002195DataSet.AcceptChanges();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (deletedPerson != null)
                    {
                        deletedPerson.Dispose();
                    }
                    if (newPerson != null)
                    {
                        newPerson.Dispose();
                    }
                    if (modifiedPerson != null)
                    {
                        modifiedPerson.Dispose();
                    }
                    MessageBox.Show("Updated");
                    this.personTableAdapter.Fill(this._30002195DataSet.Person);
                    this.teacherTableAdapter.Fill(this._30002195DataSet.Teacher);
                }


Now because I am trying to edit a certain user which requires the Person table along with the other table, it just doesn't seem to work at all.

With the datagridview, I made a new view in the sql and it has both tables combined and shows when I bring it out from the datasource, but where I go into the dataset builder and try to create an update method, all I get is the "Update" not what I would get when I created the update method from just the person's table by itself.

Can someone provide me with an example or help me out someway because I am struggling with this, I can't seem to find much information at all.

Thanks.
QuestionSaving what items are selected from a listbox. Pin
Chris-222113-Mar-15 17:13
Chris-222113-Mar-15 17:13 
AnswerRe: Saving what items are selected from a listbox. Pin
OriginalGriff14-Mar-15 0:17
mveOriginalGriff14-Mar-15 0:17 
SuggestionRe: Saving what items are selected from a listbox. Pin
Richard Deeming16-Mar-15 2:37
mveRichard Deeming16-Mar-15 2:37 
QuestionSession ended event not properly working Pin
Member 1085025313-Mar-15 11:58
Member 1085025313-Mar-15 11:58 
AnswerRe: Session ended event not properly working Pin
Eddy Vluggen13-Mar-15 12:06
professionalEddy Vluggen13-Mar-15 12:06 
GeneralRe: Session ended event not properly working Pin
Member 1085025313-Mar-15 12:19
Member 1085025313-Mar-15 12:19 
GeneralRe: Session ended event not properly working Pin
Richard Deeming16-Mar-15 2:32
mveRichard Deeming16-Mar-15 2:32 
GeneralRe: Session ended event not properly working Pin
Member 1085025313-Mar-15 12:51
Member 1085025313-Mar-15 12:51 
GeneralRe: Session ended event not properly working Pin
Eddy Vluggen14-Mar-15 3:47
professionalEddy Vluggen14-Mar-15 3:47 
QuestionError When Table has No Records ... Pin
smh139213-Mar-15 9:27
smh139213-Mar-15 9:27 
AnswerRe: Error When Table has No Records ... Pin
phil.o13-Mar-15 11:13
professionalphil.o13-Mar-15 11:13 
QuestionShow Big Number in TextBox Pin
smh139213-Mar-15 1:01
smh139213-Mar-15 1:01 
AnswerRe: Show Big Number in TextBox Pin
Pete O'Hanlon13-Mar-15 1:31
mvePete O'Hanlon13-Mar-15 1:31 
GeneralRe: Show Big Number in TextBox Pin
smh139213-Mar-15 8:27
smh139213-Mar-15 8:27 
AnswerRe: Show Big Number in TextBox Pin
OriginalGriff13-Mar-15 3:13
mveOriginalGriff13-Mar-15 3:13 
GeneralRe: Show Big Number in TextBox Pin
harold aptroot13-Mar-15 5:01
harold aptroot13-Mar-15 5:01 
QuestionProject Architecture Pin
JammoD8712-Mar-15 21:57
JammoD8712-Mar-15 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.