Hi,
I want to make enter Textbox value in multiplication of 2,the value in the textbox is retrieved from database.
This is my code:

int a = int.Parse(textBox3.Text);
int b = int.Parse(textBox3.Text);
int c = a + b;
textBox3.Text = c.ToString();

I had retrieved value from the database in Textbox as follow:

SqlDataAdapter da = new SqlDataAdapter("select * from Future where Script_Name='" + comboBox4.Text + "'", con);
            DataSet ds=new DataSet();
            da.Fill(ds ,"Future");
            foreach (DataRow r in ds.Tables["Future"].Rows)
            {
                textBox3.Text = r["Lot"].ToString();
            }

The data is successfully retrieved But,after multiplication(above code) I am getting 0 value in the Textbox,Kindly help me.

Regards,
Deven

use validating or validated event for your purpose & put this within the block.

int a = int.Parse(textBox3.Text);
int c = a*2;
textBox3.Text = c.ToString();

----------

Use sqlcommand here rather than dataadapter and dataset.

if(con.State= ConnectionState.Open)
con.Close();
con.Open();

SqlCommand cmd=new SqlCommand("select Lot from Future where Script_Name='"+
                            comboBox4.Text + "'",con);
textBox3.Text = Convert.ToString(cmd.ExecuteScalar());

Hope this helps....:D

What you did here in this code:

int a = int.Parse(textBox3.Text);
int b = int.Parse(textBox3.Text);
int c = a + b;
textBox3.Text = c.ToString();

into textBox3 comes ONE value (lets say 3), when you create two local variables (a and b) and then you sum them together and show the new value back in textBox3. This makes no sence, shouldnt be easier:


this code populated textBox with some value (one, not more) - or if there is more then one, only last will be shown:

foreach (DataRow r in ds.Tables["Future"].Rows)
            {
                textBox3.Text = r["Lot"].ToString();
            }

... now, if you want only to sum value + value, its the same as "value * 2", so you can do:

foreach (DataRow r in ds.Tables["Future"].Rows)
            {
                 int value = r["Lot"]; //if its an integer retrieved from db
                 value = value * 2;   
                 textBox3.Text = value.ToString();
            }

Hope this helps,

Mitja

Hello,
The value in the Textbox is retrieved from database as:

foreach (DataRow r in ds.Tables["Future"].Rows)
            {
                textBox3.Text = r["Lot"].ToString();
            }

I want to make Textbox accept it in 2 table(for eg : if the value retrieved is 100,it should accept 200,300,400 etc)
This is not working,Kindly help me.

Regards,
Deven.

Not understanding your requirement.

You stated "If the value retrieved is 100, textbox should except 200, 300, 400..."

So where does the multiple of 2 mentioned in your previous statement come into play?

What should the textbox except for the following retrieved values:

1) 200
2) 300
3) 400

The Textbox should accept in multiplication of 2 and so on which is retrieved from database.
So,if the value retrieved is 100,user can change it in table of 2 i.e.
200,300,400,500 & so on.If user changes the value to 150 it should not accept.
Kindly know me even if you do not understand,and help me.

Regards,
Deven.

Have you tried the Modulas Operator (%) this will return a remainder if the textBox value is NOT a multiple of the saved value?

eg

decimal _Oldvalue ;

foreach (DataRow r in ds.Tables["Future"].Rows)            
{             
_Oldvalue = Convert.ToDecimal(r["Lot"].ToString());
textBox3.Text = r["Lot"].ToString();            
}

The in your validation you could use

if (_Oldvalue % Convert.ToDecimal(textBox3.Text) ==0) // is a multiple of the oldvalue

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.