Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Console.WriteLine() in windows Forms Application

I am teaching myself basic C# with a view to develop applications that link to a SQL Server database. I am creating a very basic application that allows me type 2 values a form and on button click insert the values. I also want a separate button which shows me all the data from the table in the ConsoleWindow. The insert works perfectly, my only issue now is that when I click the ShowData button nothing happens, any ideas?

I can imagine it's going to be something pretty basic but as I said I'm still learning.

public partial class InsertNames : Form
{
    public InsertNames()
    {
        InitializeComponent();
    }

    private SqlConnection thisConnection = new SqlConnection("Data Source=(localdb)\\V11.0;database=Dev");

    private void FirstName_TextChanged(object sender, EventArgs e)
    {
        string firstName = FirstName.Text;
    }

    private void LastName_TextChanged(object sender, EventArgs e)
    {
        string lastName = LastName.Text;
    }

    private void Insert_Click(object sender, EventArgs e)
    {
        try
        {
            thisConnection.Open();
            SqlCommand insertCommand = thisConnection.CreateCommand();
            insertCommand.CommandText = "INSERT INTO Names (FirstName,LastName) Values (@FirstName, @LastName)";
            insertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = FirstName.Text;
            insertCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = LastName.Text;
            insertCommand.ExecuteNonQuery();

            Console.WriteLine(insertCommand);

            thisConnection.Close();
        }
        catch (SqlException excep)
        {
            Console.WriteLine(excep.Message);
        }
    }

    private void ShowData_Click(object sender, EventArgs e)
    {
        try
        {
            thisConnection.Open();

            SqlDataReader myReader = null;
            SqlCommand selectCommand = new SqlCommand("Select * from Names", thisConnection);

            myReader = selectCommand.ExecuteReader();
            while (myReader.Read())
            {
                Console.WriteLine(myReader["FirstName"].ToString());
                Console.WriteLine(myReader["LastName"].ToString());
            }

            thisConnection.Close();
        }
        catch (SqlException excep)
        {
            Console.WriteLine(excep.Message);
        }
    }
}
like image 579
jeastham1993 Avatar asked Sep 03 '13 21:09

jeastham1993


1 Answers

Console.WriteLine command is working only in console application type. so using windows app doesn't display required output,seems your application is windows form application.

if you want show output on Console window using Console.WriteLine withing the windows form application need to add this property and call it from the main form constructor.then it will open with console as well.

     public InsertNames()
     {
       AllocConsole();
       InitializeComponent();
     }

      [System.Runtime.InteropServices.DllImport("kernel32.dll")]
      private static extern bool AllocConsole();

OR

In project settings you can set application type as Console. Then you will get console and form as well.

like image 146
Thilina H Avatar answered Sep 25 '22 06:09

Thilina H