Friday, May 30, 2025

A network-related or instance-specific error occurred while establishing a connection to SQL Server. Error

>First open services.msc from wondow + r (Window Services)

> Then right click on sql server (SQL express)

>go to Properties

Then go to Log On tab and select Local System account

Click on Ok button and start the service.

Sunday, May 25, 2025

How to call two insert queries on one button in click in asp.net C#

 protected void Unnamed_Click(object sender, EventArgs e)

    {

        //TextBox t = DetailsView1.FindControl("TextBox1") as TextBox;

        string conStr = ConfigurationManager.ConnectionStrings["testdbConnectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection(conStr))

        {

            if (ddlSelect.SelectedValue == "1")

            {

                abc(txt1.Text, con);

            }

            if (ddlSelect.SelectedValue == "2")

            {

                abc(txt1.Text, con);

                abc(TextBox2.Text, con);

            }

}

}


void abc(string value, SqlConnection con)

    {

        SqlCommand cmd = new SqlCommand();

        cmd = new SqlCommand("INSERT INTO BookMaster(BookName) VALUES (@BookName)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@BookName", value);

        con.Open();

        cmd.ExecuteReader();

        con.Close();

    }

Friday, May 16, 2025

Restore of database failed sql server

>Open sql server

>Right Click on database >click on task>restore>database

>From device >select database bak file

>In option tab > Check overwrite the existing database (with replace)

or > in file tab click on reallocate all files to folder

Tuesday, May 13, 2025

How to collapse and expand functions of class in Visual studio

 Ctrl+M+M


Or in visual studio go to Edit and outlining and there are different shortcuts for more commands.

Saturday, May 3, 2025

How to create webapi in website without MVC Namespace - My First Webapi

 >First you have to add three files in project 1)Global.asax 2) testApiController.cs by adding web api controller class in project(add new item) 3)WebApiConfig.cs file

> Add below code in these files

  Global.asax

void Application_Start(object sender, EventArgs e) 

    {

        // Code that runs on application startup

        YashWebsiteWebForm.WebApiConfig.Register(System.Web.Http.GlobalConfiguration.Configuration);

    }


WebApiConfig.cs

 public static class WebApiConfig

    {

      

        public static void Register(HttpConfiguration config)

        {

            

            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{id}",    

                defaults: new { id = RouteParameter.Optional }

                );

        }

    }