vicserna1997 vicserna1997 - 2 years ago 55
C# Question

SQL query output not viewing on textbox

My output on SQL query is not viewing on my textbox, when the form load it must be automatically inserted to my textbox. The only output on my textbox is

System.Data.SqlClient.SqlCommand


I don't know whats wrong or missing on my codes. Please help me, sorry I'm just a newbie on c#

Any type of response is greatly appreciated. Thank you in advance.

private void EmailGen_Load(object sender, EventArgs e)
{
connect.Open();
string emailto = "select emailaddress from emails where password = ''";
string emailfr = "select emailaddress from emails where password != null";
SqlCommand emailt = new SqlCommand(emailto, connect);
SqlCommand emailf = new SqlCommand(emailfr, connect);
emailt.ExecuteNonQuery();

txBEmailRec.Text = emailt.ToString();
txBEmailFr.Text = emailf.ToString(); ;
connect.Close();

// TODO: This line of code loads data into the 'kwemDataSet.tblProducts' table. You can move, or remove it, as needed.
this.tblProductsTableAdapter.Fill(this.kwemDataSet.tblProducts);

}

Answer Source

You should use ExecuteScalar(); instead of ExecuteNonQuery(); And also, you code seems to missing to execute emailf SqlCommand.

You could see this reference as well.

private void EmailGen_Load(object sender, EventArgs e)
{
    connect.Open();
    string emailto = "select emailaddress from emails where password = ''";
    string emailfr = "select emailaddress from emails where password != null";
    SqlCommand emailt = new SqlCommand(emailto, connect);
    SqlCommand emailf = new SqlCommand(emailfr, connect);

    txBEmailRec.Text = emailt.ExecuteScalar().ToString();
    txBEmailFr.Text = emailf.ExecuteScalar().ToString();
    connect.Close();

    // TODO: This line of code loads data into the 'kwemDataSet.tblProducts' table. You can move, or remove it, as needed.
    this.tblProductsTableAdapter.Fill(this.kwemDataSet.tblProducts);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download