Thursday, November 25, 2021

Headers context json

Response.ContentType = "application/json";

or more generally

Response.Headers.Add("Content-type", "text/json");
Response.Headers.Add("Content-type", "application/json");


reference:- https://stackoverflow.com/questions/894828/creating-a-json-header-on-asp-net/894853


Webservice without using Entity, only using Datatable with JsonConvert and returning JSON (with data table)

 reference :- https://www.c-sharpcorner.com/article/how-to-convert-and-bind-json-string-in-to-asp-net-gridview-with-c-sharp/


[WebMethod]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        public string HelloWorld()

    {

        String conStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection(conStr))

        {

            using (SqlCommand cmd = new SqlCommand("Select * from aggregate._form_info", con))

            {

             

                con.Open();

                SqlDataReader dr = cmd.ExecuteReader();

                

                DataTable dt = new DataTable();

                dt.Load(dr);

             string json = JsonConvert.SerializeObject(dt);

            return json;

        //convert from JSON to object

                //DataTable myObjectDT = JsonConvert.DeserializeObject<DataTable>(json);

               // JavaScriptSerializer js1 = new JavaScriptSerializer();

                 //return JsonConvert.SerializeObject(myObjectDT);

            //without returning function, below code

                    //Context.Response.Write(json);

                con.Close();

            }

        }

    }


-------------------**************------------------------

web.config connection string

<configuration>

<connectionStrings>

<add name="connectionString" connectionString="initial catalog=aggregate;user=sa1;password=sa;data source=." />

</connectionStrings>

</configuration>

--------------------*************************--------------------------------

Output in JSON


ExecuteReader function with grid asp.net c# (Bind a grid with executereader function)

con.Open();

                    GridView1.DataSource = cmd.ExecuteReader();

                    GridView1.DataBind();

                    con.Close();

Select command (column) in Grid view asp.net C#

 <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" 

                 AutoGenerateSelectButton="True" >


                  <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />  

            </asp:GridView>

Grid view, button, textbox bootstrap class in visual studio asp.net C#

 <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 

        AutoGenerateColumns="False" DataKeyNames="ID" CssClass="table table-striped table-bordered table-condensed">

</asp:GridView>

reference = https://dotnetgenetics.blogspot.com/2014/09/aspnet-gridview-control-crud-with.html

---------------------------------*********************---------------------------------------

Bootstrap buttons

<asp:Button runat="server" ID="btnSave" Text="Save" class="btn" />

    <asp:Button runat="server" ID="btnSave1" Text="Save" class="btn btn-default" />

reference = https://www.w3schools.com/bootstrap/bootstrap_buttons.asp    

--------------------------------********************------------------------------------------

Bootstrap label and textbox

    <div class="form-group">

        <asp:Label runat="server" ID="lblName" Text="Name" AssociatedControlID="txtName" />

        <asp:TextBox runat="server" ID="txtName" class="form-control" />

    </div>

reference:- https://www.w3schools.com/bootstrap/bootstrap_forms_sizing.asp


Note:- Also you have to use default master page in asp.net application

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Grid.aspx.cs" Inherits="About" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

--------------------your code here ---------------------------

</asp:Content>