how do i fill a column in the table using cycle - c#

subj. I get the ID, and it will fill the table. But in this example, only the last entry is filled.
If you announce in page_load "pro = number" - that is filled correctly.
What to do please tell me.
public string idul;
public int pro;
public string nz;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
pro = 5;
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = pro; i > -1; i--)
{
wisp();
}
}
public void wisp()
{
SqlConnection con_tsp = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
string tsp1 = "UPDATE TSP SET IDUL=#IDUL WHERE NZ=#NZ";
SqlCommand upd_tsp = new SqlCommand(tsp1, con_tsp);
upd_tsp.Parameters.AddWithValue("#NZ", pro);
upd_tsp.Parameters.AddWithValue("#IDUL", "12");
upd_tsp.Connection.Open();
upd_tsp.ExecuteNonQuery();
upd_tsp.Connection.Close();
pro--;
}

You have set Pro as a global variable, on Button2_Click the value will be set to 0 (int's default value)
and then the button click handler will be called hence u r getting only 0 as the input value

Related

Adding multiple textbox automatically without button in C#

I have created a table and added textbox in it.
I had Use auto post back property with formula to add column 1(ratetexbox) + column 2(QTYtexbox) = column 3,now how to add the total of column 3 in a total.textbox
will adding column 1 + column 2 in column 3 I described a public void addition() and wrote the formula then in textbox of column 1 and 2 i call it.
Hence i got the result(addition) in column 3 textbox.
now i need to calculate all the 7 textbox in column 3 and add it in total.textbox without using button
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Multiply1()
{
int ma1, mb1;
bool isAVaild = int.TryParse(Rate1.Text, out ma1);
bool isBVaild = int.TryParse(Qty1.Text , out mb1);
if (isAVaild && isBVaild )
Amount1.Text = (ma1 * mb1 ).ToString();
else
Amount1.Text = "";
}
protected void Rate1_TextChanged(object sender, EventArgs e)
{
Multiply1();
}
protected void Qty1_TextChanged(object sender, EventArgs e)
{
Multiply1();
}
public void Multiply2()
{
int ma2, mb2;
bool isAVaild = int.TryParse(Rate2.Text, out ma2);
bool isBVaild = int.TryParse(Qty2.Text, out mb2);
if (isAVaild && isBVaild)
Amount2.Text = (ma2 * mb2).ToString();
else
Amount2.Text = "";
}
protected void Rate2_TextChanged(object sender, EventArgs e)
{
Multiply2();
}
public void TotalAmount()
{
int t1,t2 ;
bool isAVaild = int.TryParse(Amount1.Text , out t1);
bool isBVaild = int.TryParse(Amount2.Text , out t2);
//bool isCVaild = int.TryParse(Amount3.Text, out t3);
//bool isDVaild = int.TryParse(Amount4.Text, out t4);
//bool isEVaild = int.TryParse(Amount5.Text, out t5);
//bool isFVaild = int.TryParse(Amount6.Text, out t6);
//bool isGVaild = int.TryParse(Amount7.Text, out t7);
if (isAVaild && isBVaild )
//&& isCVaild && isDVaild && isFVaild && isGVaild )
Totalamount.Text = (t1 + t2 ).ToString();
else
Totalamount.Text = "";
}
protected void Amount1_TextChanged(object sender, EventArgs e)
{
TotalAmount();
}
protected void Amount2_TextChanged(object sender, EventArgs e)
{
TotalAmount();
}
I expected to see the output but it is not showing anything
please help me anyone

Increment number every time button is click

I want to increase an int variable i whenever I click on a button. But what I get is only int value of 1 and it doesn't increase anymore.
Here is my code:
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
By each request (Clicking on the button), a new instance will be created.
So your non-static variable will be reset to 0.
You can define i as static:
private static int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
But please note that the i variable is shared between all the users.
To improve this issue, you can use Session.
Session is an ability to store data of each user in a session.
So you can use following property to change the i variable in each session:
private int i
{
get
{
if (Session["i"] == null)
return 0;
return (int)Session["i"];
// Instead of 3 lines in the above, you can use this one too as a short form.
// return (int?) Session["i"] ?? 0;
}
set
{
Session["i"] = value;
}
}
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
As you know other answer is correct i want add another answer
You must in webform save your variables in ViewState
Just define your variables like this
public int i
{
get { Convert.ToInt32( ViewState["i"] ); }
set { ViewState["i"] = value ; }
}
Convert lblStart.Text value to int every time and assign it to i. Then increase i.
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i = Int32.Parse(lblStart.Text);
i++;
lblStart.Text = i.ToString();
}
I have similar questions as yours and I believe the issue is because the event click did not store the value that has been increased before, therefore it could not be incremented the next time you clicked, so here's my code:
protected void btn_add_Click(object sender, EventArgs e)
{
string initial;
int increment;
int quantity;
initial = TextBoxQty.Text;
increment = Convert.ToInt16(initial);
increment++;
TextBoxQty.Text = increment.ToString();
quantity = increment;
}
You can use a hidden field, initialize them to 0.
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i = int.Parse(myHiddenField.Value);
i++;
myHiddenField.Value = i;
lblStart.Text = i.ToString();
}
protected static int a = 0;
protected void btnStart_Click(object sender, EventArgs e)
{
a = a+1;
lblStart.Text = i.ToString();
}
It Works for me but on page_load() it initiates the value from 1 again !
this is actually my first time doing this
int i = 0;
while (i>=0)
{
Console.WriteLine(i);
Console.ReadLine();
i++;
}

how to get value of a selected Item in drop down list?

I need to select values from selected item in a drop down list in asp.net. in this code:
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}
But it seems that the value could not be read and so label1.text wasn't changed. how I could get the correct value of a selected item in this situation?
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
If (!IsPostback)
{
Label1.Text = Dropdownlist1.Selectedvalue;
}
}
Set AutoPosback prperty of DDL to TRUE
Use the Parse
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = int.Parse(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}

dropdown value selecting only first value

In the below C# code even though I am choosing my own year but still the value is passed as 1920 only. I can see all the values being displayed in the dropdown box but when I choose a value and submit it only 1920 is being passed to the database.
protected void Page_Load(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}
Can anyone tell me where did I do mistake?
Because when you click on the button it is again executing the code in your Page_Load event. Solution is to set the dropdown values only once. You can use Page.IsPostBack property to check whether this is the initial load or a postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
}
Try like this;
if(!IsPostBack)
[
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
Try also setting a value for the selected items like this, make sure to check for postbacks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add( new ListItem(i.ToString(), i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}

How to store record using ViewState

Now there are 2 buttons, let's call them "Button1" and "Button2" respectively.
If I click "Button1", store "1" in ViewState["Record"]; also, if I click "Button2",
store "2" in ViewState["Record"], etc.
When I click Button1⟶Button1⟶Button2⟶Button2, my expected result is: in ViewState["Record"], there are a list of records, like
[1,1,2,2].
Here is my click event code:
protected void Button1_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "1");
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "2");
}
//show the viewstate result
protected void ShowResult(object sender, EventArgs e)
{
for (int i = 1; i <= 4; i++)
{
Label.Text += ViewState["Record"];
}
}
It shows "2222" instead of "1122".
How can I code to resolve it?
When you add Record key a value actually it not adds, just puts a value Record key. You always see last added value on your loop.
You should add a List to ViewState and add value to list.
Add this property
protected List<long> ViewStateList
{
get{ if(ViewState["Record"] == null) ViewState["Record"] = new List<long>();
return (List<long>)ViewState["Record"] }
}
And use like
protected void Button1_Click(object sender, EventArgs e)
{
ViewStateList.Add(1);
}
Loop
protected void ShowResult(object sender, EventArgs e)
{
foreach(long item in ViewStateList)
{
Label.Text += item.ToString();
}
}

Categories