Home > Mobile >  Assign value to another form textbox using .net and C# and SQL server
Assign value to another form textbox using .net and C# and SQL server

Time:06-08

I want to open a form and pass value to that opened form's textbox. I tried the following code

private void UpdateTransaction(object sender, EventArgs e)
{
    if (this.dgvTransactionTable.SelectedRows.Count != 0)
    {
        DataGridViewRow row = this.dgvTransactionTable.SelectedRows[0];
        int id = Convert.ToInt32(row.Cells["id"].Value);
        SqlConnection con = new SqlConnection(connectionString);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        SqlCommand cmd = new SqlCommand("SELECT * FROM  transactions where id = '"   id   "'", con);
        SqlDataReader dt= cmd.ExecuteReader();
        if (dt.Read()) 
        {
            TransactionView transactionView = new TransactionView();
            transactionView.Show();
            transactionView.txtTransactionName = dt.GetValue(0).ToString();
            transactionView.cmbTransactionCategory = dt.GetValue(1).ToString();
            transactionView.dateTransaction = dt.GetValue(2).ToString();
            transactionView.txtTransactionName = dt.GetValue(3).ToString();
        }


    }
    else
    {
        MessageBox.Show("Please Choose a Row", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
}

but there is an error in the following section

 transactionView.txtTransactionName = dt.GetValue(0).ToString();
 transactionView.cmbTransactionCategory = dt.GetValue(1).ToString();
 transactionView.dateTransaction = dt.GetValue(2).ToString(); 
 transactionView.txtTransactionName = dt.GetValue(3).ToString();

"cannot convert type string to system windows.forms.textbox

How to fix this problem

Thank you

CodePudding user response:

You need to add .Text to your code to set the Text property of the controls.

Example:

transactionView.txtTransactionName.Text = dt.GetValue(0).ToString();
  • Related