4 Replies - 5097 Views - Last Post: 15 November 2009 - 11:49 AM Rate Topic: -----

#1 krazyk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

string to float/currency conversion in asp.net with c#

Posted 13 November 2009 - 07:40 AM

m working on asp.net with c#..initially backend is msaccess...

ma question is how to get value of currency type into textbox...

here is ma code...



 protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				con.Open(); 
 OleDbCommand comm = new OleDbCommand("SELECT * from TableData,con);
 OleDbDataReader reader = comm.ExecuteReader();

					while (reader.Read())
					{
						txtBRate.Text   =  reader[0].ToString();
				   }
				con.Close();
			  }
		 }




here in database there is one table named-TableData has one field name BasicRate-datatype is Currency

while reading data from database through datareader to textbox named-txtBRate it is giving true of false value...

i had tried to convert via Convert.ToDouble and also Convert.ToInt64 but its giving error for conversion....


plz tell me how do i get value in textbox.?


thanks in advance...


Is This A Good Question/Topic? 0
  • +

Replies To: string to float/currency conversion in asp.net with c#

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: string to float/currency conversion in asp.net with c#

Posted 13 November 2009 - 10:06 AM

What is the first column that is returned from your SQL query?

Perhaps you should either only return the column you need or specify the column name in the reader, instead of getting the 0 column. If you just return the column you need in your SQL statement then you can just use the 0 column in the reader.

txtBRate.Text   =  reader["BasicRate"].ToString();


or

 OleDbCommand comm = new OleDbCommand("SELECT BasicRate from TableData,con);

Was This Post Helpful? 1
  • +
  • -

#3 krazyk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

Re: string to float/currency conversion in asp.net with c#

Posted 14 November 2009 - 12:13 AM

hi

yes here 0 is the first column in the table...

when m reading from database like..

				txtBRate.Text=reader[0].ToString();
   


it is giving value is textbox true/false


bt after changing as u told like..

			 txtBRate.Text=reader["BasicRate"].ToString();
  



it is giving me exact value from database...


why i can't get direct reference?


thank u so much..
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: string to float/currency conversion in asp.net with c#

Posted 15 November 2009 - 11:11 AM

Its your SQL statement, you are using Select *, this indicates to return all columns from the table. There is no guarantee that the column you want will be the first one.

Lets say you had a table structure of the following:
pkUserId int PRIMARY KEY
BasicRate decimal(5,2)


By using * both columns will be returned with pkUserId in the 0 position of the OleDbDataReader and BasicRate in the 1 position.

It is always better to qualify the OleDbDataReader with the column name (it takes either an int for the index or the qualified column name as a parameter), rather than using a number. The one exception would be if you were only returning a single column in the query, in which case using 0 will work just fine.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#5 krazyk  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

Re: string to float/currency conversion in asp.net with c#

Posted 15 November 2009 - 11:49 AM

ok..

i got the point...!!

have to maintain the sequence....yes it is better to mention all filed's name rather than use "*" for selecting ...

get the difference also tht it will just carry the speed of execution while fetching the data..etc

thanks a lot...

it really helps..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1