Search code examples
c#winformstextboxuser-inputreturn-type

Text Box functions have wrong return type in Windows forms app


I've recently started learning .NET development. I was trying to make a basic SQL server connect form and I keep running into a wrong return type error for the Text Box functions that accept the username and password for the SQL server connect query.

I was trying to accept user inputs from the Text Boxes and then return their values using a variable.

private void button1_Click(object sender, EventArgs e)
{
    //These are the variables I'm trying to pass to connect functions
    string User_name = textBox1_TextChanged(sender, e);
    string Pswd = textBox2_TextChanged(sender, e);

    string connection_string = null;
    MySqlConnection conn;
    connection_string = $"server=localhost;database=practice;uid={User_name};pwd={Pswd};";
    conn = new MySqlConnection(connection_string);
    try
    {
        conn.Open();
        MessageBox.Show("Connection Established !");
        conn.Close();
    }
    catch (Exception ex) 
    {
        MessageBox.Show($"Cannot open Connection ! \nErr code : {ex}");
    }
}

//Changed return type from default void to string
public string textBox1_TextChanged(object sender, EventArgs e)
{
    string User_name = textBox1.Text;
    return User_name;
}

public string textBox2_TextChanged(object sender, EventArgs e)
{
    string Pswd = textBox2.Text;
    return Pswd;
}

This how the form looks right now. enter image description here

The Errors i get are:

  1. CS0407 'string Form1.textBox1_TextChanged(object, EventArgs)' has the wrong return type ; File = Form1.Designer.cs ; Line = 85
  2. CS0407 'string Form1.textBox2_TextChanged(object, EventArgs)' has the wrong return type ; File = Form1.Designer.cs ; Line = 99

Any help is greatly appreciated.


Solution

  • It seems like you are trying to get the values from two text boxes (textBox1 and textBox2) to use them in your SQL Server connection string. However, there are some issues with your code.

    Event handlers for the TextChanged event should return void, not string. These event handlers are meant to respond to changes in the text boxes and do not need to return values. Instead, you should access the values directly from the text boxes when needed.

    You should not call the TextChanged event handlers explicitly as you are doing here:

    string User_name = textBox1_TextChanged(sender, e);

    Instead, you should access the Text property of the text boxes directly in your button1_Click event handler.

    Here's a modified version of your code that addresses these issues:

    C#

    private void button1_Click(object sender, EventArgs e)
    {
        // Get the values from the text boxes
        string User_name = textBox1.Text;
        string Pswd = textBox2.Text;
        string connection_string = null;
        MySqlConnection conn;
        connection_string = $"server=localhost;database=practice;uid={User_name};pwd={Pswd};";
        conn = new MySqlConnection(connection_string);
        try
        {
            conn.Open();
            MessageBox.Show("Connection Established !");
            conn.Close();
        }
        catch (Exception ex) 
        {
            MessageBox.Show($"Cannot open Connection ! \nErr code : {ex}");
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // This event handler can remain as it is if you want to perform any specific actions
        // when the text in textBox1 changes.
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        // This event handler can remain as it is if you want to perform any specific actions
        // when the text in textBox2 changes.
    }
    

    In this code I changed, the button1_Click event handler directly accesses the Text property of textBox1 and textBox2 to get the values entered by the user. The TextChanged event handlers are left as they are in case you want to perform any specific actions when the text in the text boxes changes.

    If You Found This Useful Please Mark My Answer :D