Sql data Row Count Error

Posted by Shijohnjoseph under Regular Expressions on 2/24/2011 | Points: 10 | Views : 2137 | Status : [Member] | Replies : 4
Hi,
i tried to get the count of rows using dataset but what ever i did so even the count will remains '1' how can i get the exact row count
this is my code ..
please give me a solution as soon as possible

here is my code
==================
Int64 count;

if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
string ext = Path.GetExtension(FileUpload1.FileName);
if ((ext == ".doc") || (ext == ".pdf") || (ext == ".docx"))
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string connStr = ConfigurationManager.ConnectionStrings["seamanconn"].ConnectionString;
conn.ConnectionString = connStr;
cmd.Connection = conn;
cmd.CommandText = "select * from trainingfile";

SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
conn.Open();
da.SelectCommand = cmd;
da.Fill(ds);

conn.Close();
int i = ds.Tables[0].Rows.Count;
if (ds.Tables[0].Rows.Count > 0)
{
count = Convert.ToInt64(ds.Tables[0].Rows[0][0].ToString());
}
else
{
count = 0;
}
count++;
}

}

"Personality has the power to open many doors, but character must keep them open"


Responses

Posted by: Prabhakar on: 2/24/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Shijohnjoseph ,

what u want in a count ..why u using if condition ..your row count is working here

int i = ds.Tables[0].Rows.Count;

why you use

if (ds.Tables[0].Rows.Count > 0)
{
count = Convert.ToInt64(ds.Tables[0].Rows[0][0].ToString());
}
else
{
count = 0;
}
count++;
}

Best Regard's
Prabhakar

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

Posted by: Madhu.b.rokkam on: 2/24/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
you already have your count in this line

int i = ds.Tables[0].Rows.Count;

rest of the code below this is of no use.

Thanks and Regards
Madhu

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

Posted by: Karthikanbarasan on: 2/24/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

As madhu suggested you have it in that line... Is it ur questions u want to have the row number?

Thanks
Karthik
www.f5Debug.net

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

Posted by: Baijuep on: 3/1/2011 [Member] Starter | Points: 25

Up
0
Down
Select COUNT(your table name) from your databse name

The above will count the total records which are not null

Baiju EP

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

Login to post response