0

Any body please help me , how to use the Sql Statement "Select max(column.A) from TBL_table" using asp.net in C# platform and I also want the result(the maximum number ) to insert onto a text box.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by sknake
1

use this code to get max number from table in asp.net

public string login_id(string str)
    {
        
            string id;
            SqlCommand cmd = new SqlCommand("SELECT MAX(CONVERT(int, SUBSTRING(id,3,3)))+1 FROM  uploadebook", con);
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            Object obj = cmd.ExecuteScalar();
            if (obj.ToString() == "")
            {
                id = "Id1";
            }
            else
            {
                id = "Id" + obj.ToString();
            }
            con.Close();
            return id;
        
       

    }

Edited by nav33n: Please wrap your code in [code] tags for easy readability.

Votes + Comments
good one bro...it was really helpful...
0

SqlConnection and SqlCommand both implement IDisposable so care should be taking in cleaning up those resources.

using System;
using System.Data.SqlClient;

...

    private static int GetInteger()
    {
      int result;
      const string connStr = "Data Source=apex2006sql;Initial Catalog=ServManLeather;Integrated Security=True;";
      const string query = @"Select max(column.A) from TBL_table";
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          object o = cmd.ExecuteScalar();
          if (o == DBNull.Value)
            result = default(int);
          else
            result = Convert.ToInt32(o);
        }
        conn.Close();
      }
      return result;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.