Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:39:29+00:00 2026-05-28T16:39:29+00:00

I want to do some sql code run in my webservice in c# The

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:39:30+00:00Added an answer on May 28, 2026 at 4:39 pm

    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 your config file to where your <connectionStrings> section is. You will see a name=... for a connection string. Use that in your:

    ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString
    

    Example

    Here is a sample from a sample config:

    <connectionStrings>
      <add name="Sales" 
           providerName="System.Data.SqlClient"
           connectionString= "server=myserver;database=Products;uid=<user name>;pwd=<secure password>" />
    </connectionStrings>
    

    If you wanted to create a SqlConnection to the Products database, you would so this:

    SqlConnection ProdDb = 
        new SqlConnection(ConfigurationManager.ConnectionStrings["Sales"].ConnectionString);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to do some sql code run in my webservice in c# the
We have some code that we want to run a BULK INSERT sql command,
I am writing some new SQL queries and want to check the query plans
I want to run some code before an object is removed from a has_many
I want to save to a log file some SQL query rails performs, (namely
I I have a linq-to-sql database in which I want to log some of
I'm a newbie with SQL... Now I want to display some instances of AddrDistances
I Want Print DataBase Schema in Microsoft Sql Server Compact Edition. Maybe Exist Some
I want some code to be triggered every second. Usually, I'd create a Timer
I have a SQL Server 2008 DB. I want to extract SOME tables (and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.