0

I want to access the last value in database column increment it by 1 and show on a textbox I used this code but nothing is displayed on text box what's wrong with this code?

SqlCommand cmd = new SqlCommand("SELECT (MAX[Account_No]  FROM addcustomer ", my);

int result = ((int)cmd.ExecuteScalar());
textBox1.Text = result.ToString();
4
  • Is there any kind of exception? Frankly I can't see a way by now how this could go wrong without an error. (By the way, you're not incrementing result in this code. Did you spare it out for brevity or forget?) Commented Sep 4, 2013 at 8:39
  • i try it likeSqlCommand cmd = new SqlCommand("SELECT (MAX[Account_No]+1) FROM addcustomer ", my); but every time nothing displayed on textbox Commented Sep 4, 2013 at 8:45
  • SELECT MAX(Account_No) + 1 FROM addcustomer might work better. This does not solve your first problem though, so may I ask for an exception detail again? You might need to configure Visual Studio to notify you about exceptions, even if wrapped in try/catch blocks: Try Debug, Exceptions and check Thrown for Common Language Runtime Exceptions. This might show you additional information. Commented Sep 4, 2013 at 8:49
  • @user1577711 please add the code from before this block: what are you setting the value of my to? Commented Sep 4, 2013 at 9:17

5 Answers 5

2
SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No)  FROM addcustomer ", my);
int result = ((int)cmd.ExecuteScalar()) + 1;
textBox1.Text = result.ToString();
Sign up to request clarification or add additional context in comments.

5 Comments

still no working.i also try with [] brackets. no working.is some thing wrong with select query.
@user1577711 Wait a second.. What do you mean with access last value ? ExecuteScalar gets first column value of first row. Please clarify your question.
The select code you have posted should return the highest account number in your database and display that number in the text box
i want to access last record no increment it by one and show on textbox. how i can do this
@user1577711 is your last record having max Account_No ? or how you going to identify last record?
1

You have wrongly placed bracket. Change this

SqlCommand cmd = new SqlCommand("SELECT (MAX[Account_No]  FROM addcustomer ", my);

into that

SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No)  FROM addcustomer ", my);

Comments

0

you can change the sql statement as below

SELECT MAX(Account_No) + 1 FROM addcustomer

or increment after selecting value by code

SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No)  FROM addcustomer ", my);
int result = ((int)cmd.ExecuteScalar()) +1;

Final Code would be :

try
{
    using (SqlConnection con = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No) + 1 FROM addcustomer", con))
    {
        con.Open();
        int result = (int)cmd.ExecuteScalar();
        textBox1.Text = result.ToString();
    }
}
catch (SqlException ex)
{
    MessageBox.Show(ex.ToString()); // do you get any Exception here?
}

3 Comments

both are not displaying any this. is there any way to check that sql qurry is returning somthing or not.
do you have number for Account_No column?
yes after all at least display some thing on textbox. thank you dear. i am searching it for a long time
0

Looks like you're missing a bracket:

SqlCommand cmd = new SqlCommand("SELECT (MAX[Account_No]  FROM addcustomer ", my);

should be

SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No)  FROM addcustomer", my);

Comments

0
SqlCommand cmd = new SqlCommand("SELECT MAX(Account_No)  FROM addcustomer ", my);

1 Comment

this code is not workinh.i want to access last record no increment it by one and show on textbox. how i can do this

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.