Monday, April 28, 2025

Saturday, April 26, 2025

How to create unique key in SQL server management studio

 >First Open Visual Studio

>Right Click on table and click on Design

>Right Click on column name left side

>Click on Indexes/Key

>Select Unique key in 'Type' Filed


Note:- Sometime unique key option is not visible

Saturday, April 19, 2025

asp.net button pass value to url in grid view

    <asp:Button ID="Button1" runat="server"

            PostBackUrl='<%# Eval("Id","~/Employee/Update/{0}") %>'

            Text="Save" Width="75px" />

Friday, April 18, 2025

how to use webclient class uploadstring - For webapi, webservice, handler - For post and for get

webclient in asp.net 


for get

 string handlerUrl = String.Format(Request.Url.Scheme+"://" +Request.Url.Authority+"{0}","/api/values");

        string response = (new WebClient()).DownloadString(handlerUrl);


for post

   using (var client = new WebClient())

        {

            string handlerUrl = String.Format(Request.Url.Scheme + "://" + Request.Url.Authority + "{0}", "/api/values");

            client.Headers[HttpRequestHeader.ContentType] =  "application/x-www-form-urlencoded";

            var data = "=Short test...";

            string result = client.UploadString(handlerUrl, "POST", data);

            

            Response.Write(result);

        }


 https://www.aspsnippets.com/Articles/2149/Call-Consume-Web-API-using-WebClient-in-ASPNet-C/

How to use object as list of object - Array of object - How to create object in C#

   object input = new { Name ="yash"};

        input = 6;

        List<object> a = new List<object>();

        a.Add(5);

        a.Add( "ere");


object[] jji=new object[2]{ new {s = "df"}, new {s= "df"}};

some times we can use 

dr.ItemArray = new Object[] {new {    AgentName = "dfdf"}, new {AgentName = "df"} };

How to view a json file in web browser using asp.net webclient

 string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json");

 

        //Return the JSON string.

        return Content(json);

Thursday, April 17, 2025

How to create model or business object from database in visual studio asp.net - How to create Entity Data Model - DbContext class

 > Add new item in visual studio

> Add ADO.net entity data model

>Click generate from database

>Set connection string Yes or no

> Select table

>Click finish

> Click on ok when says it can harm computer

> Go to model.tt

> You can copy entity model

Note:- When creating a Entity Data Model> In the EDMX file > Goto properties and Code generation Stratigy to Default > you will see the code in the designer file(if it is showing blank file)

In web.config file connection string name will store as (use dbcontext) for remeber.

we have to use context.cs class for CRUD operation

example

//create 

using (EmployeeDBContext dbContext = new EmployeeDBContext())

        {

            dbContext.Employees.Add(employee);

            dbContext.SaveChanges();

        }

===============

//update

using (EmployeeDBContext dbContext = new EmployeeDBContext())

        {

            var entity = dbContext.Employees.FirstOrDefault(e => e.ID == id);

            entity.FirstName = employee.FirstName;

            entity.LastName = employee.LastName;

            entity.Gender = employee.Gender;

            entity.Salary = employee.Salary;

            dbContext.SaveChanges();

        }

==============

//Delete

using (EmployeeDBContext dbContext = new EmployeeDBContext())

        {

            dbContext.Employees.Remove(dbContext.Employees.FirstOrDefault(e => e.ID == id));

            dbContext.SaveChanges();

        }

============

//Getall

  using (BookMasterdbEntities db=new BookMasterdbEntities())

        {

           return db.BookMasters.ToList();

        }

==============

//Getbyid

  using (BookMasterdbEntities dbContext = new BookMasterdbEntities())

        {

            dbContext.BookMasters.add(dbContext.BookMasters.FirstOrDefault(a => a.BookID == id));

            dbContext.SaveChanges();

        }


ref:- https://dotnettutorials.net/lesson/web-api-with-sql-server/