1

I need to use this to update the note column in sql table

using (SqlConnection conn = new SqlConnection())
{
    SqlConnection connorder = new SqlConnection("Data Source=P4\\WORK;Initial Catalog=Agenda_X1;Persist Security Info=True;User ID=sa;Password=xxx");
    connorder.Open();

    string ordernrsql = lbOrderNr.Text;
    string newstate = lbNewState.Text;

    SqlCommand commandorder = new SqlCommand("UPDATE [Agenda_X1].[dbo].[Order] SET Note = @lbNewState WHERE OrderNr= @ordernumber", connorder);
    commandorder.Parameters.Add("@ordernumber", SqlDbType.NVarChar).Value = ordernrsql;
    commandorder.Parameters.Add("@lbNewState", SqlDbType.NVarChar).Value = newstate;
    connorder.Close();
}

In VS FR - SQL Commnad Visualizer looks like this

`DECLARE @ordernumber NVarChar;
SET @ordernumber = 'OPT1710420';
DECLARE @lbNewState NVarChar;
SET @lbNewState = 'Order changed 2017.06.02';

UPDATE [SAgenda_X1].[dbo].[Objednavky_ObjednavkaPrijata] SET Note= @lbNewState WHERE OrderNr = @ordernumber

But in sql this didn`t work and should be like this:

`DECLARE @ordernumber NVarChar (max);
    SET @ordernumber = 'OPT1710420';
    DECLARE @lbNewState NVarChar (max);
    SET @lbNewState = 'Order changed 2017.06.02';

    UPDATE [SAgenda_X1].[dbo].[Objednavky_ObjednavkaPrijata] SET Note= @lbNewState WHERE OrderNr = @ordernumber

also would like ask you what should i do that the finally sql query will be already with NVarChar (max)??

i already try

commandorder.Parameters.Add("@ordernumber", SqlDbType.NVarChar, -1).Value = ordernrsql;
commandorder.Parameters.Add("@lbNewState", SqlDbType.NVarChar, -1).Value = newstate;

but still sql without change.

1
  • in one query you have SAgenda_X1 and in other you have Agenda_X1 as Database name. Have you tried with only schema and table name, like UPDATE [dbo].[Order] SET Note... Commented Jun 2, 2017 at 12:40

1 Answer 1

1

Just execute the Command like commandorder.ExecuteNonQuery();

using (SqlConnection conn = new SqlConnection())
{
    SqlConnection connorder = new SqlConnection("Data Source=P4\\WORK;Initial Catalog=Agenda_X1;Persist Security Info=True;User ID=sa;Password=xxx");
    connorder.Open();

    string ordernrsql = lbOrderNr.Text;
    string newstate = lbNewState.Text;

    SqlCommand commandorder = new SqlCommand("UPDATE [Agenda_X1].[dbo].[Order] SET Note = @lbNewState WHERE OrderNr= @ordernumber", connorder);
    commandorder.Parameters.Add("@ordernumber", SqlDbType.NVarChar).Value = ordernrsql;
    commandorder.Parameters.Add("@lbNewState", SqlDbType.NVarChar).Value = newstate;

    commandorder.ExecuteNonQuery();

    connorder.Close();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.