Creating social website in asp.net - c#

I'm creating a social Networking Website for that I used "Social Networking Website in ASP.NET - Open Source" from
http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/social-networking-website-in-Asp-Net-open-source-project/
When I tried to run the project on my Visual Studio 2012 edition it is showing error like below,
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)
Source Error:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DataBaseClass
/// </summary>
public class DataBaseClass
{
SqlDataAdapter da;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public DataBaseClass()
{
//
// TODO: Add constructor logic here
//
}
public void ConnectDataBaseToInsert(string Query)
{
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet ConnectDataBaseReturnDS(string Query)
{
ds = new DataSet();
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public DataTable ConnectDataBaseReturnDT(string Query)
{
dt = new DataTable();
con = new SqlConnection(#"Data Source=Veena-SQLServer2008; Initial Catalog=RNetworkingWebApplication;");
cmd.CommandText = Query;
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return dt;
}
}
Visual studio pointing the error here da.Fill(dt).
I have spent 2 days to debug this error but still I couldn't. Please help me.

It is very simple. Your code is not able to connect to the database. Open Server Explorer in Visual Studio and connect to your database to confirm you are able to connect. And then get your connection string right.
And you do have a database, right?

Related

Database not working after publishing website on IIS - C# Web Services

Hope you're doing well!
I will try to be brief,
I made a web service called "Contacts.asmx" that have this code:
[WebMethod]
public DataTable GetContacts()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
dt.TableName = "Customers";
sda.Fill(dt);
return dt;
}
}
}
}
and and I have this code in my web.config
<add name="constr" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\user\Desktop\InternetApp\Folder\database.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
Seems to be working fine and i'm getting the XML when i click invoke.
But after publishing website on IIS : http://localhost/Contacts, i'm having this problem:
What should i do?

SSIS Script Task Error. Fill: SelectCommand.Connection property has not been initialized

I have been attempting this several different ways based on searches I have found online but am having the same result.
I am working on an SSIS Script Task that will execute a stored procedure that needs a TVP parameter to be passed into it. I have all that wired up and working properly, but when it gets to the da.Fill(resultDT)
I get the error:
Fill: SelectCommand.Connection property has not been initialized.
OleDbDataAdapter A = new OleDbDataAdapter();
DataTable dt = new DataTable();
A.Fill(dt, Dts.Variables["User::Companies"].Value);
DataTable resultDT = new DataTable();
using (SqlConnection sqlcon = (SqlConnection)(Dts.Connections["RegistryConnection"].AcquireConnection(Dts.Transaction) as SqlConnection))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = sqlcon;
cmd.CommandText = "[Registry].[GetClientData_ByCompanyIDs]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#companyIDs", dt);
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
da.Fill(resultDT);
Dts.Variables["User::Clients"].Value = resultDT;
}
}
}
Any ideas what I am missing? Hoping it's something easy that I am overlooking.
Issue was that I was using an OLEDB Connection Manager, which apparently cannot be cast to an SQLConnection in the way I was doing it.
Needed to setup ADO.Net Connection Manager and then the SQLConnection cast worked as intended.
Problem Solved.

Unable to load OleDbDataReader to DataTable

I am reading data from MS access 2010 file. I am using OleDbDataReader to read the data which is working but when I am unable to load that in DataTable .
Code:
DataTable dt = new DataTable();
string connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\My Stuff\AjaxTest & Test porjects\WbsiteWithAccessDataConnectivity\WbsiteWithAccessDataConnectivity\App_Data\Northwind 2010.accdb";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("Select * from Customers", conn))
{
using (OleDbDataReader rd = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
dt.Load(rd); // Unable to load
rd.Close();
}
conn.Close();
}
}
return dt;
Any suggestions?
I'm not exactly sure if you are getting an error or what is happening with your code but my suggestion would be to add LoadOption and/or errorHandler parameters to find out exactly what is happening.
dt.Load(rd,OverwriteChanges); // Unable to load
More info here. https://msdn.microsoft.com/en-us/library/hsze9wte%28v=vs.110%29.aspx

The connection's current state is connecting

I am writing web application in C#, and database is MSSQL Express 2012.
When i use SqlDataAdapter i get the Error:
ExecuteReader requires an open and available Connection.
The connection's current state is connecting.
SqlDataAdapter da = new SqlDataAdapter(sql, sqlConn);
da.Fill(dt);
I can not figure how to fix it. Thankss
Try this :
lock(conn)
{
DataTable dt = new DataTable();
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
{
sqlDataAdapter.Fill(dt);
}
}

C# -> Retrieving dataset from SQL Server 2008

I have a table called NAMES in my SQL Server database. I am trying to retrieve the entire table and put it into a dataset:
//get the connection string from web.config
string connString = ConfigurationManager.ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("NAMES", conn);
adapter.Fill(dataset);
}
This throws a sql exception though,
"Invalid Object Name NAMES"...
What am I doing wrong?
Open the connection !!!!!!
//get the connection string from web.config
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
DataSet dataset = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("select * from [NAMES]", conn);
conn.Open();
adapter.Fill(dataset);
}
You're not passing an actual SQL select command to the SqlCommand constructor.
First check whether Select * from dbo.[Names] is wroking in ur sql ?
string connString = ConfigurationManager .ConnectionStrings["Platform"].ConnectionString;
Dataset ds=new Dataset();
SqlConnection con = new Sqlconnection(connString);
SqlDataAdapter adapter = new SqlDataAdapter("Select * from dbo.[Names]",con);
adapter.Fill(ds);

Categories