Вопрос

I am having trouble getting the SQL ID (not an auto increment) to increment and then having that value (alphanumaric) populate a textbox. I have followed Stack Overflow for the increment and Stack Overflow for populating the value to the textbox. I do not see the ID being Incremented and my textbox is still empty.

Can some one please tell me what I am doing wrong.

This is the code I am having problems with:

    protected void Page_Load(object sender, Eventargs e)
    {
        string ID_upd = "UPDATE table SET ID = ID + 1";
        string ID_sel = "SELECT TOP 1 ID FROM table ORDER BY ID DESC";

        SqlConnection IDcon = new SqlConnection(Connection string);
        SqlCommand cmdupd = new SqlCommand(ID_upd, IDcon);
        SqlCommand cmdsel = new SqlCommand(ID_sel, IDcon);

        IDcon.Open();
        cmdupd.ExecuteNonquery();
        IDcon.Close();

        try
        {
            IDcon.Open();
            using (SqlDataReader read = cmdsel.ExecuteReader())
            {
                while (read.Read())
                {
                TextboxID.Text = (read["ID"].ToString());
                }
            }
        }
        finally
        {
            IDcon.close();
        }
    }

Thanks for the help.

Edit: I have ran this in my test page. I was successful in populating the textbox but not the update commmand. error received is: Conversion failed when converting the varchar value 'A12345' to data type int. Also this is not my last ID value.

Это было полезно?

Решение

Here is what I found to get the last ID value and get it to increment.

    protected void Page_Load(object sender, Eventargs e)
    {
        string ID_sel = "SELECT TOP 1 ID FROM table ORDER BY ID DESC";

        SqlConnection IDcon = new SqlConnection(Connection string);
        SqlCommand cmdsel = new SqlCommand(ID_sel, IDcon);

        IDcon.Open();
        SqlDataReader read = cmdsel.ExecuteReader();

        read.Read();
        string a = read["ID"].ToString();

        TextboxID.Text = ();
        for (int i = 0; i < 1; i++)
        {
            string val = a.Substring(1, a.Length - 1);
            int newnumber = Convert.ToInt32(val) + 1;
            a = "B" + newnumber.ToString("000000");
        {
        TextBoxID.Text = a;
        IDcon.Close;

Другие советы

You are using a syntax meant for numbers to 'increment' an alphanumeric string and that's why you are getting the error on update.

You cannot do that this way.

I would go with a stored procedure instead.

As a last resort that to me is pure evil are 3 statements:
1- Select to get the highest id
2- C# to split & increment the numeric part and then assemble the ID
3- Sql statement to update

But you can be sure you will run into issues when online because this solution will not handle gracefully concurrency...

I strongly suggest a stored procedure

i think also that your sql is missing a WHERE clause in the UPDATE statement: are you sure you want to overwrite each and every row in the table?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top