Hi every one,
How to solve my problem. I want Insert data into sql databse using asp.net mvc2. but i get some problem...
My asp.net mvc code....
View Form Name : FrmCompany.aspx
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Top.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
FrmCompany
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<link href="../../Content/css/FormStyle.css" rel="stylesheet" type="text/css" />
<form id="Form1" method="post" action="CompanyController/Insert" runat="server">
<% using (Html.BeginForm(null, null, FormMethod.Get, new { name = "FrmCompany", id = "Form1" }))
{ %>
<div class="form-body">
<div class="form-body-title">
<h2>Company Information</h2>
</div>
<div class="form-body-inner">
<div class="form-body-label"> Company Name </div> <input type="text" name="txtCustomerName" style="width:358px" /> <div class="br-clear"></div>
<div class="form-body-label"> Address </div> <input type="text" name="txtAddress" style="Width:358px; Height:94px;" /> <div class="br-clear"></div>
<div class="form-body-label"> Post Code </div> <input type="text" name="txtPostCode" style="width:100px" /> <div class="br-clear"></div>
<div class="form-body-label"> City </div> <input type="text" name="txtCity" style="width:132px" /> <div class="br-clear"></div>
<div class="form-body-label"> Phone </div> <input type="text" name="txtPhone" style="width:358px" /> <div class="br-clear"></div>
<div class="form-body-label"> Fax </div> <input type="text" name="txtFax" style="width:229px" /> <div class="br-clear"></div>
<div class="form-body-label"> Email </div> <input type="text" name="txtEmail" style="width:358px" /> <div class="br-clear"></div>
<div class="form-body-label"> Web </div> <input type="text" name="txtWeb" style="width:150px" />
<asp:DropDownList ID="cboStatus" runat="server">
<asp:ListItem>Head Office</asp:ListItem>
<asp:ListItem>Branch Office</asp:ListItem>
</asp:DropDownList>
<br /> <br /> <br />
<div class="form-body-label"> Look In </div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="358">
<asp:ListItem>Head Office</asp:ListItem>
<asp:ListItem>Branch Office</asp:ListItem>
</asp:DropDownList>
<br /> <br />
<div class="form-button">
<div class="form-button-text">
<input type="submit" value="Insert" id="Insert" style="width:80px;"/>
<% } %>
<input type="submit" value="Reset" style="width:80px;"/>
</div>
</div>
</div>
</div>
</form>
</asp:Content>
And Using Controller Name:
CompanyController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Bridal.Models;
namespace MVC_Bridal.Controllers
{
public class CompanyController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Insert()
{
MVC_Bridal.Models.InsertModel insertmodel = new InsertModel();
string txtCustomerName = Request.Form["txtCustomerName"];
string txtAddress = Request.Form["txtAddress"];
string txtPostCode = Request.Form["txtPostCode"];
string txtCity = Request.Form["txtCity"];
string txtPhone = Request.Form["txtPhone"];
string txtFax = Request.Form["txtFax"];
string txtEmail = Request.Form["txtEmail"];
string txtWeb = Request.Form["txtWeb"];
int _records = insertmodel.Insert(txtCustomerName, txtAddress, txtPostCode, txtCity, txtPhone, txtFax, txtEmail, txtWeb);
if (_records > 0)
{
return RedirectToAction("FrmCompany", "Home");
}
else
{
ModelState.AddModelError("", "Can Not Insert");
}
return View(insertmodel);
}
}
}
And also using Models name:
InsertModel .cs
public int Insert(string CustomerName, string Address, string PostCode, string City, string Phone, string Fax, string Email, string Web)
{
SqlConnection cn = new SqlConnection("Data Source= SERVER;Initial Catalog= Cosmic_GulshanBranch;User ID = sa; Password = 123;");
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_Company(CompanyName, CompanyAddress, PostCode, City, CompanyPhone, CompanyFax, CompanyEmail, CompanyWeb) " +
" VALUES('" + CustomerName + "','" + Address + "', '" + PostCode + "','" + City + "', '" + Phone + "','" + Fax + "', '" + Email + "','" + Web + "')", cn);
cn.Open();
return cmd.ExecuteNonQuery();
}
But do not insert data into sql database.
Problem :
The HTTP verb POST used to access path '/Home/FrmCompany/CompanyController/Insert' is not allowed.