Hi guhanath,
Please refer below sample.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
ViewBag.Data = GetData();
return View();
}
[HttpPost]
public ActionResult Index(string pricing, string rate)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UBS_Pricing(PricingId, Rates) VALUES(@PricingId, @Rates) ", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
cmd.Parameters.AddWithValue("@PricingId", Encoding.ASCII.GetBytes(pricing));
cmd.Parameters.AddWithValue("@Rates", rate);
cmd.ExecuteNonQuery();
con.Close();
}
}
ViewBag.Data = GetData();
return View();
}
public List<SelectListItem> GetData()
{
List<SelectListItem> items = new List<SelectListItem>();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [UBS_Pricing]", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
items.Add(new SelectListItem
{
Text = Encoding.ASCII.GetString((byte[])sdr["PricingId"]),
Value = Encoding.ASCII.GetString((byte[])sdr["PricingId"])
});
}
con.Close();
}
}
return items;
}
}
Namespaces
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
<tr>
<td>Pricing:</td>
<td><input type="text" id="txtpricing" name="pricing" /></td>
</tr>
<tr>
<td>Rate:</td>
<td><input type="text" id="txtRate" name="rate" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
<hr />
@Html.DropDownList("PricingId", (List<SelectListItem>)ViewBag.Data, "Please select", new { @id = "ddlPricingIds" })
</body>
</html>
SQL
CREATE TABLE [UBS_Pricing](
[PricingId] BINARY(8),
[Rates] VARCHAR(50)
)
Screenshot
Ouput