Question

I create two tables with relation one-to-many:

[dbo].[tbParent]
[ParentID] [int] IDENTITY(1,1) NOT NULL,
[ParentName] [varchar](50) NOT NULL,  

PK - ParentID

[dbo].[tbChild]
[ChildID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[ChildName] [varchar](50) NOT NULL

PK - ChildID, FK - ParentID

I created these two Stored Procedure in database. Which I want to use:

PROCEDURE [dbo].[uspUpdateData]
@ParentName NVARCHAR (50),
@ParentID INT,
@ChildName NVARCHAR (50),
@ChildID INT,
@ParentChildID INT
 AS
 BEGIN

SET NOCOUNT ON;

UPDATE tbParent
SET ParentName = @ParentName
WHERE ParentID = @ParentID

UPDATE tbChild
SET ChildName = @ChildName, ParentID = @ParentChildID
WHERE ChildID = @ChildID  
    END

and

PROCEDURE [dbo].[uspInsertData]
@ParentName NVARCHAR (50),
@ChildName  NVARCHAR (50)

AS
BEGIN

SET NOCOUNT ON;
DECLARE @RowID INT

INSERT INTO tbParent (ParentName) VALUES (@ParentName)

SELECT @RowID = SCOPE_IDENTITY()

INSERT INTO tbChild (ChildName, ParentID) VALUES (@ChildName, @RowID)

END

and here is my code:

 public partial class Form1 : Form
{
    public string connString = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
    private SqlConnection con;
    private string commandString = "SELECT * FROM view_parent_child";
    private SqlCommand cmd;
    private SqlDataAdapter da;
    private DataTable dt;
    private BindingSource bindingSource1 = new BindingSource();

    public Form1()
    {
        InitializeComponent();
        BindData();
    }

    private void BindData()
    {
        con = new SqlConnection(connString);
        cmd = new SqlCommand(commandString, con);
        da = new SqlDataAdapter(commandString, con);
        dt = new DataTable();
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);

        try
        {
            con.Open();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        //string pName = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
        //string cName = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

        con = new SqlConnection(connString);
        cmd = new SqlCommand();
        da = new SqlDataAdapter(commandString, con);
        dt = new DataTable();
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
        int childIDstatus = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[3].Value);

        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

        if (childIDstatus == 0)
        {
            try
            {
                con.Open();
                cmd.CommandText = "uspInsertData";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@ParentName", SqlDbType.NVarChar).Value = pName;
                cmd.Parameters.AddWithValue("@ChildName", SqlDbType.NVarChar).Value = cName;

                cmd.Connection = con;
                cmd.ExecuteNonQuery();

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

        }
        else
        {
            // here I will put the Input function
        }

    }

What should I do to make the program work?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top