I want to do some sql code run in my webservice in c#
The code is just
[WebMethod]
public void GetCustomers()
{
SqlConnection MyConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database1.mdf"].ConnectionString);
}
I think I got something wrong in this statement
- now my database name is:
Database1.mdf - now its table name is:
t1
I get an error like
System.NullReferenceException: Object reference not set to an instance
of an object. at WebService1.Service1.GetCustomers() in
C:\Users\PRIYANK\Documents\Visual Studio
2008\Projects\WebService1\WebService1\Service1.asmx.cs:line 36
I don’t know what to write in place of [Database1.mdf] so please write what to write in that place.
Here I place some code which might be helpful
[WebMethod]
public void GetCustomers()
{
SqlConnection MyConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database1"].ConnectionString);
MyConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = MyConn;
cmd.CommandText = "delete from t1 where name='1'";
cmd.ExecuteNonQuery();
}
You aren’t supposed to put your data file’s name in the
ConnectionString[]element place. You should be putting your ConnectionString‘s name. In other words, look in yourconfigfile to where your<connectionStrings>section is. You will see aname=...for a connection string. Use that in your:Example
Here is a sample from a sample config:
If you wanted to create a
SqlConnectionto theProductsdatabase, you would so this: