Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two tables, table details are attached here while inserting data into database foreighnkey column shows as null. Please help me.

First table
SQL
CREATE TABLE  MASTER1 ( 
ID int  (primary key), 
EMPNAME varchar(50) 
,EMPID varchar(50) ,
DEPARTMENT varchar(50)
,TITLE varchar(50) , 
CATEGORY varchar(50) ,  OTHERDETAILS varchar(50),)

second table
SQL
CREATE TABLE  addition(
ID_EMP  int (foreighnkey connect with ID of EMPMASTER),
EMP_NAME varchar(50) 
    EMP_ID varchar(50) ,
 EMP_DEPARTMENT varchar(50) )

Stored procedure
SQL
CREATE PROCEDURE [dbo].[EmployeeIns]

@EMPNAME VARCHAR(50),@EMPID VARCHAR(50),@DEPARTMENT VARCHAR(50),
@TITLE VARCHAR(500),@CATEGORY VARCHAR(500),@OTHERDETAILS varchar(500)
AS
BEGIN

INSERT INTO MASTER1(EMPNAME,EMPID,DEPARTMENT,TITLE ,CATEGORY,OTHERDETAILS)VALUES(@EMPNAME,@EMPID,@DEPARTMENT,@TITLE,@CATEGORY,@OTHERDETAILS)
END

C#
    public void fillgrid()
  
 for (int i = 0; i < GridView1.Rows.Count; i++)
      
SqlConnection SC = sqlconnection;
            SC.Open();
            string strEMPNAME = GridView1.Rows[i].Cells[0].Text.ToString().Trim();
            string strEMPID = GridView1.Rows[i].Cells[1].Text.ToString().Trim();
            string strDEPARTMENT = GridView1.Rows[i].Cells[2].Text.ToString().Trim();
            SqlCommand cmd = new SqlCommand("insert into addition (EMP_NAME,EMP_ID,EMP_DEPARTMENT) values('" + strEMPNAME + "','" + strEMPID + "','" + strDEPARTMENT + "' )", SC);
            cmd.ExecuteNonQuery();
            SC.Close();

    protected void Button1_Click(object sender, EventArgs e)
         
 SC.Open();
             SqlCommand cmd = new SqlCommand("EmployeeIns", SC);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EMPNAME", txtname.Text);
            cmd.Parameters.AddWithValue("@EMPID", txteid.Text);
            cmd.Parameters.AddWithValue("@DEPARTMENT", drpdept.SelectedValue);
            cmd.Parameters.AddWithValue("@TITLE", Txttitle.Text);
            cmd.Parameters.AddWithValue("@CATEGORY", drpcatgry.SelectedValue);
            cmd.Parameters.AddWithValue("@OTHERDETAILS", txtothers.Text);
             cmd.ExecuteNonQuery();
            fillgrid();
          sc.close();
Posted
Updated 7-May-15 1:50am
v4
Comments
Richard Deeming 7-May-15 8:34am    
Your fillgrid method is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

In fillgrid() you are inserting into ADDITION table bunch of records without specifying the value for ID_EMP (FK). Why are you surprised they are NULL? It seems your SP can insert ADDITION records if you provide those in @ADDITIONS parameter (XML). You need to decide how you want to do the insert.

EDIT
In your SP you see that addition parameters are stored in ADDITION nodes as attributes. You need to construct the XML in following form:
XML
<doc>
    <ADDITION EMP_NAME="..." EMP_ID="..." EMP_DEPARTMENT="..."/>
</doc>

Your code will look like this:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string additions = ... // construct the XML here
    SC.Open();
    SqlCommand cmd = new SqlCommand("EmployeeIns", SC);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@EMPNAME", txtname.Text);
    cmd.Parameters.AddWithValue("@EMPID", txteid.Text);
    cmd.Parameters.AddWithValue("@DEPARTMENT", drpdept.SelectedValue);
    cmd.Parameters.AddWithValue("@TITLE", Txttitle.Text);
    cmd.Parameters.AddWithValue("@CATEGORY", drpcatgry.SelectedValue);
    cmd.Parameters.AddWithValue("@OTHERDETAILS", txtothers.Text);
    cmd.Parameters.AddWithValue("@ADDITIONS", additions); // add parameter

    cmd.ExecuteNonQuery();
    fillgrid();
    sc.close();
}

And you can drop fillgrid(), it's not needed anymore.
 
Share this answer
 
v2
Comments
Tomas Takac 7-May-15 7:59am    
I updated the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900