marm saeed
marm saeed

Reputation: 11

"Method must have a return type" error in c#

It says

"Method must have a return type"

whenever I try to debug it.

I don't know how to fix this class in this line i have an error ,

    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)
        {

        }

    }
}

i tried to change to class name and add some libary but i faild i think i forget something in the class

can anybody help me please ?

Upvotes: 0

Views: 785

Answers (3)

rahulaga-msft
rahulaga-msft

Reputation: 4164

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

public event EventHandler Load;

So your handler signature should match delegate EventHandler signature. According to which it should be void

delegate void EventHandler(object sender, EventArgs e);

Upvotes: 0

Rahul
Rahul

Reputation: 77926

Your event handler doesn't specify any return type and so the error. Since it's a event handler return type must be void like

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

Upvotes: 3

Flat Eric
Flat Eric

Reputation: 8111

change

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

to

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

You are missing an important part of the method signature, the return type. Since the method should not return anything, use void

Upvotes: 3

Related Questions