Click here to Skip to main content
15,892,517 members
Home / Discussions / C#
   

C#

 
AnswerRe: SerialPort Pin
Rajesh Anuhya20-Oct-10 20:10
professionalRajesh Anuhya20-Oct-10 20:10 
GeneralRe: SerialPort Pin
Tichaona J21-Oct-10 3:41
Tichaona J21-Oct-10 3:41 
QuestionC# Generics help Pin
gi122620-Oct-10 5:47
professionalgi122620-Oct-10 5:47 
AnswerRe: C# Generics help PinPopular
harold aptroot20-Oct-10 6:06
harold aptroot20-Oct-10 6:06 
GeneralRe: C# Generics help Pin
gi122621-Oct-10 2:59
professionalgi122621-Oct-10 2:59 
QuestionVS designer (C#) Pin
kapax520-Oct-10 4:53
kapax520-Oct-10 4:53 
QuestionHow to sniff traffic of only one application Pin
rahul.kulshreshtha20-Oct-10 4:05
rahul.kulshreshtha20-Oct-10 4:05 
QuestionProblem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 0:47
Hardz20-Oct-10 0:47 
Hi,

I am using c# and sql for this simple example and I have two related tables: Master, Details. The Primary Key in Master is MasterId and there is a Foreign Key in Details table to establish the relation. The columns of Master are bound to textbox while the Details table are bound to datagridview. MasterBindingSource and DetailBindingSource are generated automatically. My problem with this is that adding a new Master along with its Details, coz it errors with FK constraint violated. Actually, when I add a new Master, the MasterId value @ the textbox is -1, so the value that was recognized by the Details MasterId @ the datagridview is -1 likewise, so when the insertion is updated to the database, MasterId is automatically set by the db engine due to its identity property. However, this MasterId is not known to the dataset. So when the Details table is updated to the database, the error occurs(Sql Exception was unhandled). So I need help for this problem coz I tried everything but nothing happened.

Anyway, here is my simple 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 MasterDetail
{
public partial class Form1 : Form
{
//DsMasterDetail ds = new DsMasterDetail();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.masterTableAdapter.Fill(this.dsMasterDetail.Master);
this.detailTableAdapter.Fill(this.dsMasterDetail.Detail);
}
void Master_RowChanged(object sender, DataRowChangeEventArgs args)
{

}

private void masterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
Validate();
masterBindingSource.EndEdit();
masterTableAdapter.Update(this.dsMasterDetail.Master);

dgvDetail.Refresh();
detailBindingSource.EndEdit();
detailTableAdapter.Update(this.dsMasterDetail.Detail);
}

private void dgvDetail_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
//e.Row.Cells[1].Value = bnPositionItem.Text;
}
}
}

Thanks in advance, your help will be greatly appreciated.
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
rah_sin20-Oct-10 1:07
professionalrah_sin20-Oct-10 1:07 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 14:39
Hardz20-Oct-10 14:39 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 15:27
Hardz20-Oct-10 15:27 
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
PIEBALDconsult20-Oct-10 3:21
mvePIEBALDconsult20-Oct-10 3:21 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 14:33
Hardz20-Oct-10 14:33 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
PIEBALDconsult21-Oct-10 3:06
mvePIEBALDconsult21-Oct-10 3:06 
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz28-Oct-10 23:56
Hardz28-Oct-10 23:56 
QuestionNeed help Pin
rksreadero19-Oct-10 16:33
rksreadero19-Oct-10 16:33 
AnswerRe: Need help Pin
JF201519-Oct-10 18:15
JF201519-Oct-10 18:15 
AnswerRe: Need help Pin
Abhinav S19-Oct-10 23:18
Abhinav S19-Oct-10 23:18 
GeneralRe: Need help Pin
Nish Nishant20-Oct-10 4:04
sitebuilderNish Nishant20-Oct-10 4:04 
GeneralRe: Need help Pin
Keith Barrow20-Oct-10 7:58
professionalKeith Barrow20-Oct-10 7:58 
AnswerRe: Need help Pin
Ravi Bhavnani20-Oct-10 4:53
professionalRavi Bhavnani20-Oct-10 4:53 
Question{"Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'. "} Pin
Mohammad Dayyan19-Oct-10 15:25
Mohammad Dayyan19-Oct-10 15:25 
QuestionExecuteNonQuery problem? [modified] Pin
Emmet_Brown19-Oct-10 8:21
Emmet_Brown19-Oct-10 8:21 
AnswerRe: ExecuteNonQuery problem? Pin
Luc Pattyn19-Oct-10 8:33
sitebuilderLuc Pattyn19-Oct-10 8:33 
AnswerRe: ExecuteNonQuery problem? Pin
Maciej Los19-Oct-10 8:42
mveMaciej Los19-Oct-10 8:42 

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.