C#C
C#3y ago
446 replies
yumo

❔ SELECT MAX ID from textbox

Why is this displaying this error?

System.FormatException: 'Incorrectly formatted input string.'

This is my table sql
CREATE TABLE [dbo].[tbl_acoes] (
    [acaoID]        INT            NOT NULL,
    [data]          NVARCHAR (MAX) NULL,
    [local]         NVARCHAR (MAX) NULL,
    [indice]        NVARCHAR (MAX) NULL,
    [desc]          NVARCHAR (MAX) NULL,
    [foto]          IMAGE          NULL,
    [estado]        NVARCHAR (MAX) NULL,
    [observacoes]   NVARCHAR (MAX) NULL,
    [validacao]     NVARCHAR (MAX) NULL,
    [ensaioID]      INT            NULL,
    [idProjetos]    INT            NULL,
    [descricaoacao] NVARCHAR (MAX) NULL,
    [nensaio]       INT            NOT NULL,
    PRIMARY KEY CLUSTERED ([acaoID] ASC),
    CONSTRAINT [FK_tbl_acoes_tbl_ensaios] FOREIGN KEY ([ensaioID]) REFERENCES [dbo].[tbl_ensaios] ([ensaioID])
);


This is the code im using and what i want is select the max id of apples in the project number that is in the textbox
private void InserirIDEnsaios()
        {
            int novoID;
            string query = "SELECT MAX(nensaio) FROM tbl_acoes WHERE idProjetos = '" + txtNIDPROJETOS.Text + "'";
            SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\infprog\\Desktop\\Patcher\\Oil\\bin\\Debug\\oil.mdf;Integrated Security=True");
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    con.Open();
                    novoID = int.Parse(cmd.ExecuteScalar().ToString()) + 1;
                    txtEnsaioID.Text = novoID.ToString();
                }
                con.Close();
            }
        }
image.png
Was this page helpful?