How to insert multiple records in Items table but only one in Master table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omaragainonthetrack
    New Member
    • Jul 2009
    • 2

    How to insert multiple records in Items table but only one in Master table

    public void InsertPlannedMa terialMasterReq uest(int id, string requestDocument , string requestDescript ion, string requestType, string hodApproval, int companyCode, string hodRemarks, string det_Description , string name, int qty, string code)
    {
    string query = "INSERT INTO MasterRequest (ID,Req_Documen t,Req_Descripti on,ReqType,HODA pproval,Company Code,HODRemarks ) VALUES (@ID,@Req_Docum ent,@Req_Descri ption,@ReqType, @HODApproval,@C ompanyCode,@HOD Remarks)";

    SqlConnection con = new SqlConnection(c onstr);
    SqlCommand com = new SqlCommand(quer y, con);
    com.Parameters. Add("@ID", SqlDbType.Int). Value = id;
    com.Parameters. Add("@Req_Docum ent", SqlDbType.NVarC har).Value = requestDocument ;
    com.Parameters. Add("@Req_Descr iption", SqlDbType.NVarC har).Value = requestDescript ion;
    com.Parameters. Add("@ReqType", SqlDbType.NVarC har).Value = requestType;
    com.Parameters. Add("@HODApprov al", SqlDbType.NVarC har).Value = hodApproval;
    com.Parameters. Add("@CompanyCo de", SqlDbType.Int). Value = companyCode;
    com.Parameters. Add("@HODRemark s", SqlDbType.NVarC har).Value = hodRemarks;

    con.Open();
    com.ExecuteNonQ uery();
    con.Close();


    string query2 = "INSERT INTO DetailRequest (ID,Req_Documen t,Description,A pproval) VALUES (@ID,@Req_Docum ent,@Descriptio n,@Approval)";
    SqlConnection con2 = new SqlConnection(c onstr);
    SqlCommand com2 = new SqlCommand(quer y2, con2);
    com2.Parameters .Add("@ID", SqlDbType.Int). Value = id;
    com2.Parameters .Add("@Req_Docu ment", SqlDbType.NVarC har).Value = requestDocument ;
    com2.Parameters .Add("@Descript ion", SqlDbType.NVarC har).Value = det_Description ;
    //com2.Parameters .Add("@Quantity ", SqlDbType.Int). Value = qty;
    //com2.Parameters .Add("@UOM", SqlDbType.NVarC har).Value = unitOfMeasure;
    com2.Parameters .Add("@Approval ", SqlDbType.NVarC har).Value = hodApproval;

    con2.Open();
    com2.ExecuteNon Query();
    con2.Close();

    string query3 = "INSERT INTO ITEMS (ID,Code,Name,Q uantity) VALUES (@ID,@Code,@Nam e,@Quantity)";
    SqlConnection con3 = new SqlConnection(c onstr);
    SqlCommand com3 = new SqlCommand(quer y3, con3);
    com3.Parameters .Add("@ID", SqlDbType.Int). Value = id;
    com3.Parameters .Add("@Code", SqlDbType.NVarC har).Value = code;
    com3.Parameters .Add("@Name", SqlDbType.NVarC har).Value = name;
    com3.Parameters .Add("@Quantity ", SqlDbType.Int). Value = qty;

    con3.Open();
    com3.ExecuteNon Query();
    con3.Close();


    }

    //Now following is the code I am using in my PlannedmterialR equest.aspx.cs

    protected void Button1_Click(o bject sender, EventArgs e)
    {
    MasterRequest_D AL helper = new MasterRequest_D AL();

    PlannedMaterial Request pnd = new PlannedMaterial Request();
    ItemsHandling itm = new ItemsHandling() ;

    string[] labelName = new string[100];
    string[] coder = new string[100];
    string[] quantity = new string[100];


    if (TextBox6.Text != "0")//if quantity entered into Capacitor textBox
    {
    labelName[1] = itm.Code(0).ToS tring();
    labelName[2] = TextBox6.Text;
    helper.InsertPl annedMaterialMa sterRequest(Con vert.ToInt32(Te xtBox1.Text), TextBox2.Text, TextBox4.Text, DropDownList6.S electedValue, DropDownList2.S electedValue, Convert.ToInt32 (TextBox11.Text ), TextBox12.Text, TextBox13.Text, Label8.Text, Convert.ToInt32 (TextBox6.Text) , labelName[1]);


    }

    if (TextBox12.Text != "0")//if quantity entered into Wires textBox
    {
    //labelName(0) needs to be commented
    //labelName[0] = pnd.Label8.Text .ToString();
    labelName[1] = itm.Code(1).ToS tring();
    labelName[2] = TextBox12.Text;
    helper.InsertPl annedMaterialMa sterRequest(Con vert.ToInt32(Te xtBox1.Text), TextBox2.Text, TextBox4.Text, DropDownList6.S electedValue, DropDownList2.S electedValue, Convert.ToInt32 (TextBox11.Text ), TextBox12.Text, TextBox13.Text, Label12.Text, Convert.ToInt32 (TextBox8.Text) , labelName[1]);



    }
    }

    The problem I am facing is that using the above code. The ID goes multiple times in MasterRequest table which should go only once, while the same ID must go to Items table multiple times as per the quantities being entered. I want only the ID to go into MasterRequest table just once not multiple times.
    Attached Files
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Originally posted by omaragainonthet rack
    public void InsertPlannedMa terialMasterReq uest(int id, string requestDocument , string requestDescript ion, string requestType, string hodApproval, int companyCode, string hodRemarks, string det_Description , string name, int qty, string code)
    {
    string query = "INSERT INTO MasterRequest (ID,Req_Documen t,Req_Descripti on,ReqType,HODA pproval,Company Code,HODRemarks ) VALUES (@ID,@Req_Docum ent,@Req_Descri ption,@ReqType, @HODApproval,@C ompanyCode,@HOD Remarks)";

    SqlConnection con = new SqlConnection(c onstr);
    SqlCommand com = new SqlCommand(quer y, con);
    com.Parameters. Add("@ID", SqlDbType.Int). Value = id;
    com.Parameters. Add("@Req_Docum ent", SqlDbType.NVarC har).Value = requestDocument ;
    com.Parameters. Add("@Req_Descr iption", SqlDbType.NVarC har).Value = requestDescript ion;
    com.Parameters. Add("@ReqType", SqlDbType.NVarC har).Value = requestType;
    com.Parameters. Add("@HODApprov al", SqlDbType.NVarC har).Value = hodApproval;
    com.Parameters. Add("@CompanyCo de", SqlDbType.Int). Value = companyCode;
    com.Parameters. Add("@HODRemark s", SqlDbType.NVarC har).Value = hodRemarks;

    con.Open();
    com.ExecuteNonQ uery();
    con.Close();


    string query2 = "INSERT INTO DetailRequest (ID,Req_Documen t,Description,A pproval) VALUES (@ID,@Req_Docum ent,@Descriptio n,@Approval)";
    SqlConnection con2 = new SqlConnection(c onstr);
    SqlCommand com2 = new SqlCommand(quer y2, con2);
    com2.Parameters .Add("@ID", SqlDbType.Int). Value = id;
    com2.Parameters .Add("@Req_Docu ment", SqlDbType.NVarC har).Value = requestDocument ;
    com2.Parameters .Add("@Descript ion", SqlDbType.NVarC har).Value = det_Description ;
    //com2.Parameters .Add("@Quantity ", SqlDbType.Int). Value = qty;
    //com2.Parameters .Add("@UOM", SqlDbType.NVarC har).Value = unitOfMeasure;
    com2.Parameters .Add("@Approval ", SqlDbType.NVarC har).Value = hodApproval;

    con2.Open();
    com2.ExecuteNon Query();
    con2.Close();

    string query3 = "INSERT INTO ITEMS (ID,Code,Name,Q uantity) VALUES (@ID,@Code,@Nam e,@Quantity)";
    SqlConnection con3 = new SqlConnection(c onstr);
    SqlCommand com3 = new SqlCommand(quer y3, con3);
    com3.Parameters .Add("@ID", SqlDbType.Int). Value = id;
    com3.Parameters .Add("@Code", SqlDbType.NVarC har).Value = code;
    com3.Parameters .Add("@Name", SqlDbType.NVarC har).Value = name;
    com3.Parameters .Add("@Quantity ", SqlDbType.Int). Value = qty;

    con3.Open();
    com3.ExecuteNon Query();
    con3.Close();


    }

    //Now following is the code I am using in my PlannedmterialR equest.aspx.cs

    protected void Button1_Click(o bject sender, EventArgs e)
    {
    MasterRequest_D AL helper = new MasterRequest_D AL();

    PlannedMaterial Request pnd = new PlannedMaterial Request();
    ItemsHandling itm = new ItemsHandling() ;

    string[] labelName = new string[100];
    string[] coder = new string[100];
    string[] quantity = new string[100];


    if (TextBox6.Text != "0")//if quantity entered into Capacitor textBox
    {
    labelName[1] = itm.Code(0).ToS tring();
    labelName[2] = TextBox6.Text;
    helper.InsertPl annedMaterialMa sterRequest(Con vert.ToInt32(Te xtBox1.Text), TextBox2.Text, TextBox4.Text, DropDownList6.S electedValue, DropDownList2.S electedValue, Convert.ToInt32 (TextBox11.Text ), TextBox12.Text, TextBox13.Text, Label8.Text, Convert.ToInt32 (TextBox6.Text) , labelName[1]);


    }

    if (TextBox12.Text != "0")//if quantity entered into Wires textBox
    {
    //labelName(0) needs to be commented
    //labelName[0] = pnd.Label8.Text .ToString();
    labelName[1] = itm.Code(1).ToS tring();
    labelName[2] = TextBox12.Text;
    helper.InsertPl annedMaterialMa sterRequest(Con vert.ToInt32(Te xtBox1.Text), TextBox2.Text, TextBox4.Text, DropDownList6.S electedValue, DropDownList2.S electedValue, Convert.ToInt32 (TextBox11.Text ), TextBox12.Text, TextBox13.Text, Label12.Text, Convert.ToInt32 (TextBox8.Text) , labelName[1]);



    }
    }

    The problem I am facing is that using the above code. The ID goes multiple times in MasterRequest table which should go only once, while the same ID must go to Items table multiple times as per the quantities being entered. I want only the ID to go into MasterRequest table just once not multiple times.
    Hi omaragainonthet rack,

    Welcome to the forums.

    To help your question to get answered, please take a look at the Posting Guidelines.

    It will help us to help you if you reformat your post using code tags. It makes it much easier to read. It might be helpful as well if you post the question before the code sample.

    Kindest regards,
    Matt.

    Comment

    Working...