Contact Form
How fill Drop down list in angular 9 with api Asp.net pi
CREATE TABLE [dbo].[tblSkills](
[SkillID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Title] [varchar](50) NULL,
[isacrtive] [bit] NULL,
PRIMARY KEY CLUSTERED
(
[SkillID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tblSkills] ON
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (1, N'Visual Foxpro', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (2, N'C#', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (3, N'VB.NET', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (4, N'Delphi', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (5, N'Java', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (6, N'Power Builder', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (7, N'COBOL', NULL)
INSERT [dbo].[tblSkills] ([SkillID], [Title], [isacrtive]) VALUES (8, N'Python', NULL)
SET IDENTITY_INSERT [dbo].[tblSkills] OFF
Scaffold-DbContext "Server=.;Database=MbkTest;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables tblSkills -Force
TblSkillsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CurdAPP2.Models;
namespace CurdAPP2.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TblSkillsController : ControllerBase
{
private readonly MbkTestContext _context;
public TblSkillsController(MbkTestContext context)
{
_context = context;
}
// GET: api/TblSkills
[HttpGet]
public async Task<ActionResult<IEnumerable<TblSkills>>> GetTblSkills()
{
return await _context.TblSkills.ToListAsync();
}
// GET: api/TblSkills/5
[HttpGet("{id}")]
public async Task<ActionResult<TblSkills>> GetTblSkills(int id)
{
var tblSkills = await _context.TblSkills.FindAsync(id);
if (tblSkills == null)
{
return NotFound();
}
return tblSkills;
}
// PUT: api/TblSkills/5
[HttpPut("{id}")]
public async Task<IActionResult> PutTblSkills(int id, TblSkills tblSkills)
{
if (id != tblSkills.SkillId)
{
return BadRequest();
}
_context.Entry(tblSkills).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TblSkillsExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/TblSkills
[HttpPost]
public async Task<ActionResult<TblSkills>> PostTblSkills(TblSkills tblSkills)
{
_context.TblSkills.Add(tblSkills);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTblSkills", new { id = tblSkills.SkillId }, tblSkills);
}
// DELETE: api/TblSkills/5
[HttpDelete("{id}")]
public async Task<ActionResult<TblSkills>> DeleteTblSkills(int id)
{
var tblSkills = await _context.TblSkills.FindAsync(id);
if (tblSkills == null)
{
return NotFound();
}
_context.TblSkills.Remove(tblSkills);
await _context.SaveChangesAsync();
return tblSkills;
}
private bool TblSkillsExists(int id)
{
return _context.TblSkills.Any(e => e.SkillId == id);
}
}
}
Angular
skillsM.ts
blog-post.service.tsexport class skillsM { skillId?: number; title: string; isacrtive?: number; }
getskills(): Observable<skillsM[]> {
return this.http.get<skillsM[]>(this.myAppUrl + 'api/TblSkills/')
.pipe(
retry(1),
catchError(this.errorHandler)
);
}
public skillsMs: Observable<skillsM[]>;
this.skillsMs = this.blogPostService.getskills();
<select class="form-control" [formControl]="skillId">
<option value="0">--Select--</option>
<option *ngFor="let blogPost of (skillsMs | async)"
value={{blogPost.skillId}}>
{{blogPost.title}}
</option>
</select>
Insert Update Delete Drop down list
---------------BE--------------
public class BEToday
{
public int Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
----------------Dal-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using BE_Today;
namespace DAL_Today
{
public class DALToday
{
private static string connect = MyConnectionString.path;
public List<BEToday> show()
{
List<BEToday> list=new List<BEToday>();
SqlConnection con= new SqlConnection(connect);
SqlCommand cmd=new SqlCommand(Myproc.Name.spshow.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
BEToday be = new BEToday();
be.Id = Convert.ToInt32(sdr["Id"]);
be.Name=Convert.ToString(sdr["Name"]);
be.Address = Convert.ToString(sdr["Address"]);
list.Add(be);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
return list;
}
public void insert( BEToday be)
{
SqlConnection con= new SqlConnection(connect);
SqlCommand cmd=new SqlCommand(Myproc.Name.spinsert.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
cmd.Parameters.AddWithValue("@Name",be.Name);
cmd.Parameters.AddWithValue("@Address",be.Address);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
public void update(BEToday be)
{
SqlConnection con = new SqlConnection(connect);
SqlCommand cmd = new SqlCommand(Myproc.Name.spupdate.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
cmd.Parameters.AddWithValue("@Name", be.Name);
cmd.Parameters.AddWithValue("@Address", be.Address);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
public void delte(BEToday be)
{
SqlConnection con = new SqlConnection(connect);
SqlCommand cmd = new SqlCommand(Myproc.Name.spdelete.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
------------------------bl--------------------
public List<BEToday> show()
{
return new DALToday().show();
}
public void insert(BEToday be)
{
new DALToday().insert(be);
}
public void update(BEToday be)
{
new DALToday().update(be);
}
public void delte(BEToday be)
{
new DALToday().delte(be);
}
-------------------test.aspx---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="testasp.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>Insert</h1></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtinsertname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtnsertaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text=" Save " OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>update</h1></td>
</tr>
<tr>
<td><asp:Label ID="Label5" runat="server" Text="Select Value"></asp:Label></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtupdatename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtupdateaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text=" Update " OnClick="btnUpdate_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>Delete</h1></td>
</tr>
<tr>
<td><asp:Label ID="Label6" runat="server" Text="Select Value"></asp:Label></td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtdletename0" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label8" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtdeleteaddress0" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnDelete" runat="server" Text=" Delete " OnClick="btnDelete_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br /> <br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
-----------------------------------test.aspx.cs-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_Today;
using BL_Today;
namespace testasp
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databnd();
dllupdate();
dlldelete();
}
}
public void databnd()
{
GridView1.DataSource = new BLToday().show();
GridView1.DataBind();
}
public void dllupdate()
{
DropDownList1.DataSource = new BLToday().show();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "----select----");
}
public void dlldelete()
{
DropDownList2.DataSource = new BLToday().show();
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Id";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "----select----");
}
protected void Button1_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Name = txtinsertname.Text;
be.Address = txtnsertaddress.Text;
new BLToday().insert(be);
databnd();
dllupdate();
dlldelete();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
var n = new BLToday().show().Single(id=>id.Id==Convert.ToInt32(DropDownList1.SelectedValue));
txtupdatename.Text = n.Name;
txtupdateaddress.Text = n.Address;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Id=Convert.ToInt32(DropDownList1.SelectedValue);
be.Name=txtupdatename.Text;
be.Address=txtupdateaddress.Text;
new BLToday().update(be);
databnd();
dllupdate();
dlldelete();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedIndex > 0)
{
var n = new BLToday().show().Single(id => id.Id == Convert.ToInt32(DropDownList2.SelectedValue));
txtdletename0.Text = n.Name;
txtdeleteaddress0.Text = n.Address;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Id = Convert.ToInt32(DropDownList2.SelectedValue);
new BLToday().delte(be);
databnd();
dllupdate();
dlldelete();
}
}
}
public class BEToday
{
public int Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
----------------Dal-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using BE_Today;
namespace DAL_Today
{
public class DALToday
{
private static string connect = MyConnectionString.path;
public List<BEToday> show()
{
List<BEToday> list=new List<BEToday>();
SqlConnection con= new SqlConnection(connect);
SqlCommand cmd=new SqlCommand(Myproc.Name.spshow.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
BEToday be = new BEToday();
be.Id = Convert.ToInt32(sdr["Id"]);
be.Name=Convert.ToString(sdr["Name"]);
be.Address = Convert.ToString(sdr["Address"]);
list.Add(be);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
return list;
}
public void insert( BEToday be)
{
SqlConnection con= new SqlConnection(connect);
SqlCommand cmd=new SqlCommand(Myproc.Name.spinsert.ToString(),con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
cmd.Parameters.AddWithValue("@Name",be.Name);
cmd.Parameters.AddWithValue("@Address",be.Address);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
public void update(BEToday be)
{
SqlConnection con = new SqlConnection(connect);
SqlCommand cmd = new SqlCommand(Myproc.Name.spupdate.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
cmd.Parameters.AddWithValue("@Name", be.Name);
cmd.Parameters.AddWithValue("@Address", be.Address);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
public void delte(BEToday be)
{
SqlConnection con = new SqlConnection(connect);
SqlCommand cmd = new SqlCommand(Myproc.Name.spdelete.ToString(), con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", be.Id);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
}
}
------------------------bl--------------------
public List<BEToday> show()
{
return new DALToday().show();
}
public void insert(BEToday be)
{
new DALToday().insert(be);
}
public void update(BEToday be)
{
new DALToday().update(be);
}
public void delte(BEToday be)
{
new DALToday().delte(be);
}
-------------------test.aspx---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="testasp.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>Insert</h1></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtinsertname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtnsertaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text=" Save " OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>update</h1></td>
</tr>
<tr>
<td><asp:Label ID="Label5" runat="server" Text="Select Value"></asp:Label></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtupdatename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtupdateaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text=" Update " OnClick="btnUpdate_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<table class="auto-style1">
<tr >
<td colspan="2" align="center"><h1>Delete</h1></td>
</tr>
<tr>
<td><asp:Label ID="Label6" runat="server" Text="Select Value"></asp:Label></td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtdletename0" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label8" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtdeleteaddress0" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnDelete" runat="server" Text=" Delete " OnClick="btnDelete_Click" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<br /> <br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
-----------------------------------test.aspx.cs-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_Today;
using BL_Today;
namespace testasp
{
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databnd();
dllupdate();
dlldelete();
}
}
public void databnd()
{
GridView1.DataSource = new BLToday().show();
GridView1.DataBind();
}
public void dllupdate()
{
DropDownList1.DataSource = new BLToday().show();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "----select----");
}
public void dlldelete()
{
DropDownList2.DataSource = new BLToday().show();
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Id";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "----select----");
}
protected void Button1_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Name = txtinsertname.Text;
be.Address = txtnsertaddress.Text;
new BLToday().insert(be);
databnd();
dllupdate();
dlldelete();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex > 0)
{
var n = new BLToday().show().Single(id=>id.Id==Convert.ToInt32(DropDownList1.SelectedValue));
txtupdatename.Text = n.Name;
txtupdateaddress.Text = n.Address;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Id=Convert.ToInt32(DropDownList1.SelectedValue);
be.Name=txtupdatename.Text;
be.Address=txtupdateaddress.Text;
new BLToday().update(be);
databnd();
dllupdate();
dlldelete();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.SelectedIndex > 0)
{
var n = new BLToday().show().Single(id => id.Id == Convert.ToInt32(DropDownList2.SelectedValue));
txtdletename0.Text = n.Name;
txtdeleteaddress0.Text = n.Address;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
BEToday be = new BEToday();
be.Id = Convert.ToInt32(DropDownList2.SelectedValue);
new BLToday().delte(be);
databnd();
dllupdate();
dlldelete();
}
}
}
Subscribe to:
Posts
(
Atom
)