2

So I am trying to have a button when clicked it sends data to an sql server. I thought i could just create a form that would only have a submit button and I would handle that submit button in my home controller.

My html is as such:

<body>
<div  style="border: solid; max-width: 300px; margin-left: auto; margin-right: auto">


    @using(Html.BeginForm())
    {

    <input type="submit" value="Vote"/>
    }

</div>



</body>

in my home controller i thought it was supposed to handle the form such as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;

namespace WAgermanClub.Controllers
{
public class HomeController : Controller
{


[HttpPost]
    public ActionResult add(string vote1)
    {




        SqlConnection vote1connection = new SqlConnection("user id=userid;" +
                               "password=validpassword;server=o5z5dpwpzi.database.windows.net;" +
                               "Trusted_Connection=yes;" +
                               "database=wagermanclub_votes; " +
                               "connection timeout=30");
        try
        {
            vote1connection.Open();
        }
        catch (Exception g)
        {
            Console.WriteLine(g.ToString());
        }

        try
        {
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from table", vote1connection);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {

                Console.WriteLine(myReader["Vote1"].ToString());
            }
        }
        catch (Exception i)
        {
            Console.WriteLine(i.ToString());
        }


        SqlCommand vote1command = new SqlCommand("INSERT INTO table (Column1, Vote1) " +
                              "Values (1, 'Vote1' + 1)", vote1connection);


        vote1command.ExecuteNonQuery();

        try
        {
            vote1connection.Close();
        }
        catch (Exception h)
        {
            Console.WriteLine(h.ToString());
        }






    }
}
}

It all looks fine except for I get an error saying that in the add(string vote1) not all code paths return a value. What am I doing wrong? I do not have that great of a grip on html forms but I feel that once I get this it will lead to a better understanding.

All help appreciated! Thanks!

3
  • this doesn't have anything to do with your html. Commented Sep 10, 2013 at 0:13
  • 3
    As the error clearly states, your C# function needs to return a value. Commented Sep 10, 2013 at 0:18
  • @SLaks could I have an example? Commented Sep 10, 2013 at 0:23

1 Answer 1

3

This;

public ActionResult add(string vote1)

Means you've declared a method that returns an ActionResult variable. There is no return statement in your method. Once you fix this, the error will be gone.

Sign up to request clarification or add additional context in comments.

1 Comment

ahhhhhhhhhhhh, oh my I should have seen that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.