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!