"方法必须有一个返回类型" c#中的错误

时间:2018-03-17 14:33:44

标签: c#

它说

  

"方法必须有一个返回类型"

每当我尝试调试它时。

我不知道如何解决这门课程 在这一行我有一个错误,

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //btn_Submit Click event

       Form1_Load(object sender, System.EventArgs e)
        {
            // Do whatever
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string d, y, z;
            d = (textBox1.Text);
            y = (textBox2.Text);
            if (d == "" || y == "")
            {
                MessageBox.Show("ERROR");
                return;
            }
            try
            {
                //Create SqlConnection
                SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;");
                SqlCommand cmd = new SqlCommand("Select * from Table_1 where id=@d and password=@y", con);
                cmd.Parameters.AddWithValue("@username", d);
                cmd.Parameters.AddWithValue("@password", y);
                con.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;
                //If count is equal to 1, than show frmMain form



                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");
                    this.Hide();
                    frmMain fm = new frmMain();
                    fm.Show();
                }
                else
                {
                    MessageBox.Show("Login Failed!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    private void label1_Click_1(object sender, EventArgs e)
        {

        }

    }
}

我试图更改为类名并添加一些库,但我发现了 我想我在课堂上忘记了什么

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

变化

Form1_Load(object sender, System.EventArgs e)
{
    // Do whatever
}

void Form1_Load(object sender, System.EventArgs e)
{
    // Do whatever
}

您缺少方法签名的重要部分,即返回类型。由于该方法不应返回任何内容,因此请使用void

答案 1 :(得分:2)

您的事件处理程序未指定任何返回类型,因此错误。因为它是一个事件处理程序,所以返回类型必须是void,如

private void Form1_Load(object sender, System.EventArgs e)

答案 2 :(得分:0)

Form1_Load is handler which you subscribed to Load event of form. 

public event EventHandler Load;

因此,您的处理程序签名应与委托EventHandler签名匹配。根据它应该是void

delegate void EventHandler(object sender, EventArgs e);