views:

65

answers:

1

I am using C#, Visual Studio 2005, and SQL Server 2000.

My problem is as below. Here is my code:

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((Keys)e.KeyChar == Keys.Enter)
    {
        foreach (Control abcl  in this.flowLayoutPanel1.Controls)
        {
            if (abcl.Name.Contains("bb") && abcl  is TextBox)
            {
                int indx = this.flowLayoutPanel1.Controls.Count-1;
                for (int i = 0; i < indx; i++)
                {
                    string mess = "Sorry Empty.Records";
                    if (this.flowLayoutPanel1.Controls[i].Text == "")
                    {
                        MessageBox.Show(mess);
                        mess = null;
                    }
                    else
                    {
                        string connstr = "server=.;initial catalog= myDataBase  pwd=mypasward";
                        SqlConnection con = new SqlConnection(connstr);
                        con.Open();

                        SqlCommand cmd1 = new SqlCommand("insert into debankA(companyID,transID,date,bank,totdepo,narrat) values " +
                        "(@companyID,@transID,Convert(datetime,'" + maskedTextBox1.Text.ToString() + "',103),@bank,@totdepo,@narrat)", con);
                        cmd1.Parameters.Add("@bank", SqlDbType.VarChar).Value = textBox1.Text;
                        cmd1.Parameters.Add("@totdepo", SqlDbType.Decimal).Value = label13.Text;
                        cmd1.Parameters.Add("@narrat", SqlDbType.VarChar).Value = textBox2.Text;
                        cmd1.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
                        cmd1.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
                        cmd1.ExecuteNonQuery();

                        string pparticulars = null;
                        double? depo = null;
                        string messs = "Record Save Successfully";
                        foreach (Control ctl in this.flowLayoutPanel1.Controls)
                        {
                            if (ctl.Name.Contains("tbb") && ctl is TextBox)
                            {
                                pparticulars = ctl.Text;
                            }

                            if (ctl.Name.Contains("bb") && ctl is TextBox)
                            {
                                double ddepo = 0;

                                if (double.TryParse(ctl.Text, out ddepo))
                                    depo = ddepo;

                                if (pparticulars != null && depo != null)
                                {
                                    SqlCommand cmd = new SqlCommand("insert into debankB(particulars,deposit,companyID,transID)values" +
                                    "(@particulars,@deposit,@companyID,@transID)", con);
                                    cmd.Parameters.Add("@particulars", SqlDbType.VarChar).Value = pparticulars;
                                    cmd.Parameters.Add("@deposit", SqlDbType.Decimal).Value = depo;
                                    cmd.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
                                    cmd.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
                                    cmd.ExecuteNonQuery();
                                    pparticulars = null;
                                    depo = null;

                                    MessageBox.Show("Record Saved Successfully");
                                    messs = null;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The above is multiple textboxes projects on flowlayoutpanel1. There are two textboxes named "tbb" and "bb". The problem is that the above works fine but there is just the problem of the message show as much as time as total textboxes in controls. The else condition in this code does not work well. That's why both messages show instead of one condition true. I am not familiar with flowlayoutpanel.

A: 

Its because you have the outer loop looping through all controls on the screen. This will loop through both text boxes on the screen. Then within that loop you are going through all the text boxes again - so you will be validating the bb text box twice.

Vixen
@vixen thx for ur feedback sir can u guide me how to do it?. Please guide me by coding sir.
mahesh
@vixen my requirement is only to check bb text only not tbb. only one runtime textboxes.
mahesh
@Vixen sir how to solve it?.
mahesh