Name:-Zain Ul Abdin Section: - BSIT (6C) Arid No: - 2018-Arid-1340 Submitted To: - MR Ahsan Quiz No: - 02

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Name:- Zain Ul Abdin

Section:- BSIT(6C)
Arid No:- 2018-Arid-1340
Submitted To:- Mr Ahsan
Quiz No:- 02
Controller

using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication88.Models;
using WebApplication88.DAL;
namespace WebApplication8.Controllers
{
public class UserController : Controller
{
// GET: User
public ActionResult Index()
{

return View();
}
[HttpPost]
public ActionResult Index(User user)
{
ViewBag.isPost = true;

UserEntity userEntity = new UserEntity();


int rowsEffected = userEntity.insert(user);

if (rowsEffected > 0)
{
ViewBag.db = true;
}
else
{
ViewBag.db = false;
}
return View();
}
}
}

View
@model WebApplication88.Models.User

<h2>Index</h2>
<body>
@using (Html.BeginForm())
{
<table>
<tr>
<td> Title </td>
<td> @Html.TextBoxFor(x=>x.title) </td>
</tr>
<tr>
<td> Category </td>
<td> @Html.TextBoxFor(x => x.category) </td>
</tr>

<tr>
<td> Publish Type </td>
<td> @Html.TextBoxFor(x => x.pbt) </td>
</tr>
<tr>
<td><input type="submit" value="Add User" /></td>
</tr>
</table>}
@if (ViewBag.isPost != null)
{
<p>Data Submitted Successfully</p>}
@if (ViewBag.db != null && ViewBag.db)
{
<p>Data Saved Successfully</p>}

</body>

MODEl
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication88.Models
{
public class User {
public string title { get; set; }
public string category { get; set; }
public int pbt { get; set; }

}
}

Class
using System;
using System.Data.SqlClient;
using WebApplication88.Models;
namespace WebApplication88.DAL
{
public class UserEntity
{
string ConnectionString = @"Data Source=DESKTOP-L0E8OUH;Initial
Catalog=AuthorBooks;Integrated Security=True";
public int insert(User user)
{
int rowsEffected = 0;
try
{
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();

string Query = "INSERT INTO Books(title,category,publishyear)" +


"values ('" + user.title + "','" + user.category + "','"
+ user.pbt +"')";
SqlCommand cmd = new SqlCommand(Query, conn);
rowsEffected = cmd.ExecuteNonQuery();
conn.Close();
return rowsEffected;
}
catch (Exception e)
{
return rowsEffected;
}
}
}
}

You might also like