Hi venkatg,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE [dbo].[Students](
[StudentId] [int] NOT NULL,
[StudentName] [varchar](50) NULL,
[Status] [char](1) NULL
) ON [PRIMARY]
GO
INSERT INTO Students VALUES (1,'Ram','P')
INSERT INTO Students VALUES (2,'Ajay','P')
INSERT INTO Students VALUES (3,'Rahim','A')
INSERT INTO Students VALUES (4,'Alex','P')
INSERT INTO Students VALUES (5,'Anwar','P')
Model
public class StudentModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
}
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View(new StudentModel());
}
[HttpPost]
public ActionResult Index(string id)
{
return View(GetStudentById(id));
}
public StudentModel GetStudentById(string id)
{
DataSet ds = null;
StudentModel cobj = null;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT StudentId,StudentName,Status FROM Students WHERE StudentId = @Id", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cobj = new StudentModel();
cobj.Id = Convert.ToUInt16(ds.Tables[0].Rows[i]["StudentId"].ToString());
cobj.Name = ds.Tables[0].Rows[i]["StudentName"].ToString();
cobj.Status = ds.Tables[0].Rows[i]["Status"].ToString();
}
return cobj;
}
catch (Exception ex)
{
return cobj;
}
finally
{
con.Close();
}
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_989874_Button_Visible.Models.StudentModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<% using (Html.BeginForm("Index", "Home", FormMethod.Post))
{ %>
<fieldset>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Id:</td>
<td><%: Model.Id %></td>
</tr>
<tr>
<td>Name:</td>
<td><%: Model.Name %></td>
</tr>
<tr>
<td>Status</td>
<td><%: Model.Status %></td>
</tr>
</table>
</fieldset>
<input type="text" name="id" value="1" />
<input type="submit" value="Download" />
<br />
<%if (Model.Status == "P")
{ %>
<button type="submit" value="Approve" name="button" class="btn btn-info">
Approve</button>
<button type="submit" value="Reject" name="button" class="btn btn-warning">
Reject</button>
<% } %>
<% } %>
</body>
</html>
Screenshot