I am a new developer and trying to develop a web service in C# by following this tutorial. I did everything as explained in that tutorial, however, I did not get any data from the Northwind database and I got the following page when I pressed the Invoke button:
As you will see in the tutorial, I did not add the ConnectionString to the web.config file. Should I do that?
My code:
public class WSGetCustomerCountryWise : System.Web.Services.WebService
{
public WSGetCustomerCountryWise()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod(Description = "It will generate Customer List, CountryWise")]
public System.Xml.XmlElement
GetCustomerCountryWise(string sCountry)
{
string sConn = ConfigurationManager.ConnectionStrings["connStr"].ToString();
string sSQL = "select CustomerId, CompanyName, ContactTitle, City from Customers where country = '"+sCountry+"'";
SqlConnection connCustomer = new SqlConnection(sConn);
DataSet dsCustomer = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sSQL, sConn);
sda.Fill(dsCustomer);
System.Xml.XmlDataDocument xdd = new System.Xml.XmlDataDocument(dsCustomer);
System.Xml.XmlElement docElem = xdd.DocumentElement;
return docElem;
}
}
You are trying to load the connection string with
ConfigurationManager.ConnectionStrings["connStr"].ToString(), but you didn’t add it to the configuration (=web.config), so yes you should do that.