Bring a newly added ID to put in a textbox c# windows form

Asked

Viewed 405 times

2

I request a help to do the following, I do an Insert via Procedure after saving I want to fill the textbox with the id created so I can use it to associate to my item inserir a descrição da imagem aqui

I just need the order code to enter the textbox. Thanks for your help. New button event

 private void btnNovoPedido_Click(object sender, EventArgs e)
    {

        try
        {
            int codigoCli = Convert.ToInt32(txtCodCliente.Text);
            DateTime dtIni = DateTime.Now;
            int idCliente = int.Parse(txtCodCliente.Text);
            cc.pInserirPedido(0, dtIni, null, idCliente); //procedure para criar pedido
            MessageBox.Show("Pedido gerado" );


        }
        catch (Exception ex)
        {
            MessageBox.Show("Erro:" + ex.Message);
        }


    }

ALTER PROC [dbo]. [pInserirPedido]
@decimal value (8,2),
@dtEntrada datetime,
@dtSaida datetime,
@idCliente int
as
Insert into TB_PEDIDOS(VAL_PEDIDO,
HOR_PEDIDO_ENTRADA,HOR_PEDIDO_SAIDA,ID_CLIENTE)
values(@valueTotal, @dtEntrate, @dtSaida, @idCliente) IF @@ERROR<>0
-- SELECT -1
will return -1 if there is error ELSE
SELECT MAX(ID_PEDIDIO) as 'CURRENT ORDER' FROM TB_PEDIDOS
GO
the last ID added

inserir a descrição da imagem aqui

so I still don’t know how to get the value displayed in sql and play in the field inserir a descrição da imagem aqui

  • Where’s the Insert code? Where’s the button code?

  • I added the new button event but I didn’t add the to put in the textbox because I don’t know how to make talusing Iqueryable but I don’t know how to use only I can use if the ID is in a grid ai so I recover the same value here: int code = Convert.Toint32(dgvCliente.CurrentRow.Cells[0].Value.Tostring()); txtCodigo.Text = code.Tostring(); example of a value I picked from the grid .

  • cc.pInserirPedido returns some value?

  • Not only does it actually insert a new order so after I will add items to the order by another proc more so I need to receive the created order id

  • enter the code of cc.pInserirPedido

  • I put proc there in question

  • Is the database SQL Server? If it is, you have even considered using SELECT @@Identity how do PROC return? Something else, what is the type of the variable cc?

  • Ola Fernando CC is the instance of the class Conexaoclientedatacontext which I use to do anything in the database I am using LINQ to SQL classes . I didn’t try wouldn’t I use it after inserting ? how can I use it if I’m already searching about

  • I did what you advised in proc it is bringing the last id added but I am just checking in sql server the return of select more to implement in c# not yet because I do not know how to get the return of this select

Show 4 more comments

1 answer

0


Hello I found the solution simpler than I ever imagined follows solution

 private void btnNovoPedido_Click(object sender, EventArgs e)
    {
        pInserirPedidoResult con = new pInserirPedidoResult();

        try
        {
            int codigoCli = Convert.ToInt32(txtCodCliente.Text);
            DateTime dtIni = DateTime.Now;
            int idCliente = int.Parse(txtCodCliente.Text);
            cc.pInserirPedido(0, dtIni, null, idCliente);
            MessageBox.Show("Pedido gerado");
            txtIDPedido.Text = cc.TB_PEDIDOs.Max(x => x.ID_PEDIDO).ToString(); <--- // AQUI A SOLUÇÃO !!! Achei já perdendo a esperança ufa   traz o id recem criado                
        }
        catch (Exception ex)
        {
            MessageBox.Show("Erro:" + ex.Message);
        }

             }

source: https://www.devmedia.com.br/forum/max-com-linq/439548

Browser other questions tagged

You are not signed in. Login or sign up in order to post.