1

Hello guys I have got this SqlDataAdapter which finds the highest value:

DataTable dt = new DataTable();             
SqlDataAdapter SDA = new SqlDataAdapter("Select MAX (ID_K) FROM klient", spojeni);
SDA.Fill(dt);


spojeni.Close();

If it selects I need its result add +1 and insert it as object value into this parameter.

prikaz2.Parameters.AddWithValue("@val4", ); // here should be SDA +1

I tried to insert to object value whole SqlDataAdapter but it was nonsense. Is there any way to achieve that?

Thanks in advance

4
  • 2
    SELECT ... and ExecuteNonQuery? Commented Jul 17, 2013 at 12:40
  • Yes, that is nonsense. What makes you think the SqlDataAdapter is a number? Commented Jul 17, 2013 at 13:01
  • @catfood 14 days learning how to c#/sql :-D ? Commented Jul 17, 2013 at 13:06
  • That would do it! Try one step at a time. :-) Commented Jul 17, 2013 at 13:14

2 Answers 2

3

You can use SqlCommand.ExecuteScalar() method for getting this value because;

Executes the query, and returns the first column of the first row in the result set returned by the query.

Since your command returns only maximum value of ID_K column, that is exactly what we want.

SqlCommand command = new SqlCommand("Select MAX (ID_K) FROM klient");
SqlDataAdapter SDA = new SqlDataAdapter(command, spojeni);
int max = (int)command.ExecuteScalar();
prikaz2.Parameters.AddWithValue("@val4", max + 1);
Sign up to request clarification or add additional context in comments.

Comments

3
int maxID = (int)comm.ExecuteScalar();
prikaz2.Parameters.AddWithValue("@val4", maxID + 1);

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.