I have two tables, table details are attached here while inserting data into database foreighnkey column shows as null. Please help me.
First table
Hide Copy Code
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
Hide Copy Code
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
Hide Copy Code
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
Hide Expand Copy Code
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();