Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
No errors and warnings but i am not getting the output.

showing:

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131831092354667571): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[14544] iisexpress.exe' has exited with code -1 (0xffffffff).

What I have tried:

namespace Emp
{
public partial class DBfetch : System.Web.UI.Page
{
public static int i;
SqlConnection conn=null;
SqlDataAdapter da;
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
string connectionstring = "Data Source=DESKTOP-KR73POJ;Initial Catalog=Test; Persist Security Info=True;User ID=sa;Password=admin";
conn = new SqlConnection(connectionstring);
conn.Open();
da = new SqlDataAdapter("select * from testtable", conn);
da.Fill(dt);
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
i = 0;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}
protected void Button2_Click(object sender, EventArgs e)
{
if (i == 0)
Response.Write("First record !");
else
i--;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}
protected void Button3_Click(object sender, EventArgs e)
{
if (i == (dt.Rows.Count - 1))
Response.Write("Last record !");
else
i++;
dr = dt.Rows[i];
TextBox1.Text = Convert.ToString(dr[0]);
TextBox2.Text = Convert.ToString(dr[1]);
TextBox3.Text = Convert.ToString(dr[2]);
}

}

}
Posted
Updated 3-Oct-18 21:25pm

1 solution

Look at the error message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
It's pretty clear: it can't find the SQL server instance you are referring to.
Which means your connection string is wrong.
Ignoring that you shouldn;t hardcode connection strings, they should be in config files so you don't have to change the app each time you release it...

Start here: Simple SQL Connection String Creation[^] and make sure the connection works before you start to code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900