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.

Recommended Answers

All 2 Replies

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;
        
       

    }
commented: 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;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.