Subscribe to Subscribers

Forums » .NET » ASP.NET »

Conversion problem help me


Posted Date: 19 Jun 2012      Posted By:: saravanakumar     Member Level: Gold    Member Rank: 196     Points: 5   Responses: 4



Dear Sir,


con.Open();
SqlDataAdapter da = new SqlDataAdapter("select sum(stockqty) from stock where mcode='" + ddl_matcode.SelectedItem.Text.Trim() + "'", con);
DataSet ds = new DataSet();
da.Fill(ds, "table");
con.Close();

this code returns stockqty value means no problem. If is is null means the following error occur.

if (ds.Tables[0].Rows.Count == 0)
{
total_stock_qty = Convert.ToDouble(txt_matqty.Text.Trim());
}
else if (ds.Tables[0].Rows.Count != 0)
{
total_stock_qty = Convert.ToDouble(ds.Tables[0].Rows[0][0].ToString()) + Convert.ToDouble(txt_matqty.Text.Trim());
}


ds.Tables[0].Rows[0][0].ToString() this value is null means conversion error will be displayed. If you have any idea.

I have also try this,

Double.parse(ds.Tables[0].Rows[0][0].ToString())


if (ds.Tables[0].Rows[0][0].ToString() == null)
{
total_stock_qty = Convert.ToDouble(txt_matqty.Text.Trim());
}
else if (ds.Tables[0].Rows[0][0].ToString()!= null)
{
total_stock_qty = Convert.ToDouble(ds.Tables[0].Rows[0][0].ToString()) + Convert.ToDouble(txt_matqty.Text.Trim());
}


System.FormatException: Input string was not in a correct format. at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Convert.ToDouble(String value) at Material.Storagelocation.bt_store_add_Click(Object sender, EventArgs e) in D:\saravanakumar\Material_grn\Material\Storagelocation.aspx.cs:line 233


please give me idea how slove this problem.




Responses

#676358    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 17     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,

It simply means that there is no value at ds.Tables[0].Rows[0][0].

You can check this value for null before performing calculation:


If(ds.Tables[0].Rows[0][0].ToString() != String.Empty)
{
total_stock_qty = Convert.ToDouble(ds.Tables[0].Rows[0][0].ToString()) + Convert.ToDouble(txt_matqty.Text.Trim());
}


Hope it'll help you.
Regards
Ajatshatru


 
#676360    Author: Mahesh Durgam      Member Level: Gold      Member Rank: 137     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

ToString() function will never convert 'null' values, you will get null exception.

You have to use Convert.ToString() function, which has ability to convert null to empty space. Once you have converted you can compare it will empty space and then assign the value.


if (Convert.ToString(ds.Tables[0].Rows[0][0]) == "")
{
total_stock_qty = Convert.ToDouble(........);
}
else if (Convert.ToString(ds.Tables[0].Rows[0][0])!= "")
{
total_stock_qty = Convert.ToDouble(Convert.ToString(ds.Tables[0].Rows[0][0])) + .....
}



Thanks,
Mahesh


 
#676363    Author: saravanakumar      Member Level: Gold      Member Rank: 196     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

ya it's working fine.

thank u frnd.





 
#676376    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 20/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Try adding a NULL check as below.


if (ds.Tables[0].Rows.Count == 0)
{
if(txt_matqty.Text.Trim()!="")
{
total_stock_qty = Convert.ToDouble(txt_matqty.Text.Trim());
}
}
else if (ds.Tables[0].Rows.Count != 0)
{
if(txt_matqty.Text.Trim()!="" && ds.Tables[0].Rows[0][0].ToString()!="" )
{
total_stock_qty = Convert.ToDouble(ds.Tables[0].Rows[0][0].ToString()) + Convert.ToDouble(txt_matqty.Text.Trim());
}
}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Multiple Selection in Dropdown and send from quesry string according to it.
Previous : Paypall sandbox integration
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.