简体   繁体   中英

how to get data to textboxes from database by enter event of on another textbox with if condition in c#

how to get data to textbox by enter event of on another textbox with if condition in c#

I have been trying to get data from sql database by pressing Enter key of a text box, when i enter some wrong text, it doesn't popup or message to wrong entry.

private void txtProdId_TextChanged(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(connStrng))
    {
        con.Open();
        String strSQL = "SELECT ProdName, Volume, CostPrice From tblProduct Where ProdCode=@ProdCode";
        using (SqlCommand cmd = new SqlCommand(strSQL, con))
        {
            cmd.Parameters.AddWithValue("@ProdCode", txtProdId.Text);
            using (SqlDataAdapter DA = new SqlDataAdapter(cmd))
            {
                DA.SelectCommand = cmd;
                try
                {
                    DataSet DS = new DataSet();
                    DA.Fill(DS);

                    foreach (DataRow row in DS.Tables[0].Rows)
                    {
                        txtProdName.Text = row["ProdName"].ToString();
                        txtProdVol.Text = row["Volume"].ToString();
                        txtProdPrice.Text = row["CostPrice"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

could anyone help me to get result.

can you try something like:

private void txtProdId_TextChanged(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == Keys.Enter)
    {

        using (SqlConnection con = new SqlConnection(connStrng))
        {
            con.Open();
            String strSQL = "SELECT ProdName, Volume, CostPrice From tblProduct Where ProdCode=@ProdCode";
            using (SqlCommand cmd = new SqlCommand(strSQL, con))
            {
                cmd.Parameters.AddWithValue("@ProdCode", txtProdId.Text);
                using (SqlDataAdapter DA = new SqlDataAdapter(cmd))
                {
                    DA.SelectCommand = cmd;
                    try
                    {
                        DataSet DS = new DataSet();
                        DA.Fill(DS);

                        foreach (DataRow row in DS.Tables[0].Rows)
                        {
                            txtProdName.Text = row["ProdName"].ToString();
                            txtProdVol.Text = row["Volume"].ToString();
                            txtProdPrice.Text = row["CostPrice"].ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }

Use it in Keydown events:

I guess this is the code you wanted.

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            using (SqlConnection con = new SqlConnection(connStrng))
            {
                con.Open();
                String strSQL = "SELECT ProdName, Volume, CostPrice From tblProduct Where ProdCode=@ProdCode";
                using (SqlCommand cmd = new SqlCommand(strSQL, con))
                {
                    cmd.Parameters.AddWithValue("@ProdCode", txtProdId.Text);
                    using (SqlDataAdapter DA = new SqlDataAdapter(cmd))
                    {
                        DA.SelectCommand = cmd;
                        DataSet DS = new DataSet();
                        DA.Fill(DS);
                        try
                        {

                            if (DS.Tables.Count > 0)
                            {
                                if (DS.Tables[0].Rows.Count > 0)
                                {
                                    foreach (DataRow row in DS.Tables[0].Rows)
                                    {
                                        txtProdName.Text = row["ProdName"].ToString();
                                        txtProdVol.Text = row["Volume"].ToString();
                                        txtProdPrice.Text = row["CostPrice"].ToString();
                                    }
                                }
                                else
                                {
                                    txtProdName.Text = String.Empty;
                                    txtProdVol.Text = String.Empty;
                                    txtProdPrice.Text = String.Empty;
                                    MessageBox.Show("No record found!");
                                }
                            }
                            else
                            {
                                MessageBox.Show("No record found!");
                            }


                        }
                        catch (Exception er)
                        {
                            MessageBox.Show(er.Message);
                        }
                    }
                }
            }
        }
    }

No need to TextChanged event. You can use KeyPress event for TextBox.

if(e.KeyCode==Keys.Enter){
  if(textBox1.Text = "some string"){
    //your code here...
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM