Question

My first question to be asked on stackoverflow so I hope that I am asking correctly. I have found this link C# .NET4.0 TableAdapter.Update() won't insert new record

And I am trying to expand on this and return the Identity of the record that was inserted. I know that this can be done but and having some issues with the syntax in C#.

BLL for insert

[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public int InsertNewTicket(string ticket, DateTime sub_date, string sub_ADID, bool completed, DateTime? comp_Date, bool Bw, string bW_ADID, bool c_Back)
{
    ServiceDesk.SDDataTable InsertNew = new ServiceDesk.SDDataTable();
    ServiceDesk.SDRow NewTicketValues = InsertNew.NewSDRow();        
    NewTicketValues.Ticket = ticket;
    NewTicketValues.Sub_date = sub_date;
    NewTicketValues.Sub_ADID = sub_ADID;
    NewTicketValues.Completed = completed;
    if (comp_Date == null) NewTicketValues.SetComp_DateNull(); else NewTicketValues.Comp_Date = comp_Date.Value;
    NewTicketValues.BW = Bw;
    NewTicketValues.BW_ADID = bW_ADID;
    NewTicketValues.C_Back = c_Back;
    InsertNew.AddSDRow(NewTicketValues);    
    int rowsAffected = Adapter.Update(InsertNew);
    return rowsAffected;
}

and this is the insert at the UI

 protected void Sub_Click(object sender, EventArgs e)
{
    SDBLL insernewticketadapter = new SDBLL();

    string ticket = TicketNumberBox.Text;
    DateTime sub_date = DateTime.Now;
    string sub_ADID = Server.HtmlEncode(Request.Cookies["UserInfo"]["userADID"]);
    bool completed = false;
    DateTime? comp_Date = null;
    bool Bw = false;
    string bW_ADID = null;
    bool c_Back = false;
    int newSDID;
    try
    {
        newSDID = insernewticketadapter.InsertNewTicket(ticket, sub_date, sub_ADID, completed, comp_Date, Bw, bW_ADID, c_Back);

        idofnewrecord.Text = Convert.ToString(newSDID);
    }
    catch (ArgumentException ae)
    {
        idofnewrecord.Text = ae.Message;
    }


}  

Its very simple I just want to in the UI return the ID of the record that was inserted. Dataset insert is

INSERT INTO [OKC_CALL].[SD].[SD] ([Ticket], [Sub_date], [Sub_ADID], [Completed], [Comp_Date], [BW], [BW_ADID], [C_Back]) 
VALUES (@Ticket, @Sub_date, @Sub_ADID, @Completed, @Comp_Date, @BW, @BW_ADID, @C_Back);

SELECT SCOPE_IDENTITY()

So how do I get the Scope_Identity in my UI ?

No correct solution

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