how to get max value

Posted by Aspbimal under C# on 12/26/2013 | Points: 10 | Views : 1749 | Status : [Member] | Replies : 6
how to get max value from data table in SQL using C#




Responses

Posted by: vishalneeraj-24503 on: 12/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Use Datatable Select method to get max record as

datarow[] dr = dt.select("id = max(id)");

//dt is your datatable object.

if(dr!=null)
{
int max = convert.toint32(dr.copytodatatable);
}

Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aspbimal on: 12/26/2013 [Member] Starter | Points: 25

Up
0
Down
code to get max value from C#

Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 12/26/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Can you elaborate your question in more specific?


Thank you,
Govind

Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You can also use dataview to get max record.

dataview dv = dt.defaultview;
dataview.rowfilter = "max(id)";
dt = dv.totable();



Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Try the below code:-

var con = ConfigurationManager.ConnectionStrings["Yourconnection"].ToString();

using (SqlConnection myConnection = new SqlConnection(con))
{
string oString = "Select max(id) as id from your_table_name";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
string max = dr["id"].ToString()
}

myConnection.Close();
}
}


Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Have you try my code?

Aspbimal, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response