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 8400213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:30:21+00:00 2026-06-09T21:30:21+00:00

I have an ASP.Net website which is connected to an SQL Server. In a

  • 0

I have an ASP.Net website which is connected to an SQL Server. In a previous project (VB) I used the following code to write to my database:

Dim connectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
                Dim insertSql As String = "INSERT INTO tblProfile(UserID, UserName, Title, FirstName, LastName, MiddleName, DateofBirth, Gender, HomePhoneNumber, MobilePhoneNumber, Address, StreetName, StreetType, Suburb, PostCode, State, Country) VALUES(@UserID, @UserName, @Title, @FirstName, @LastName, @MiddleName, @DateofBirth, @Gender, @HomePhoneNumber, @MobilePhoneNumber, @Address, @StreetName, @StreetType, @Suburb, @PostCode, @State, @Country)"

                Using myConnection As New SqlConnection(connectionString)
                    myConnection.Open()
                    Dim myCommand As New SqlCommand(insertSql, myConnection)
                    myCommand.Parameters.AddWithValue("@UserID", newUserGuid)
                    myCommand.Parameters.AddWithValue("@UserName", newUserName)
                    myCommand.Parameters.AddWithValue("@Title", Title.SelectedItem.Text)
                    myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
                    myCommand.Parameters.AddWithValue("@LastName", LastName.Text)
                    If MiddleNames.Text = String.Empty Then
                        myCommand.Parameters.AddWithValue("@MiddleName", DBNull.Value)
                    Else
                        myCommand.Parameters.AddWithValue("@MiddleName", MiddleNames.Text)
                    End If
                    DateofBirth.Text = YearofBirth.Text + "-" + MonthofBirth.Text + "-" + DayofBirth.Text
                    myCommand.Parameters.AddWithValue("@DateofBirth", DateofBirth.Text)
                    myCommand.Parameters.AddWithValue("@Gender", Gender.SelectedItem.Text)
                    If HomePhoneNumber.Text = String.Empty Then
                        myCommand.Parameters.AddWithValue("@HomePhoneNumber", DBNull.Value)
                    Else
                        myCommand.Parameters.AddWithValue("@HomePhoneNumber", HomePhoneNumber.Text)
                    End If
                    If MobilePhoneNumber.Text = String.Empty Then
                        myCommand.Parameters.AddWithValue("@MobilePhoneNumber", DBNull.Value)
                    Else
                        myCommand.Parameters.AddWithValue("@MobilePhoneNumber", MobilePhoneNumber.Text)
                    End If
                    myCommand.Parameters.AddWithValue("@Address", AddressNumber.Text)
                    myCommand.Parameters.AddWithValue("@StreetName", StreetName.Text)
                    myCommand.Parameters.AddWithValue("@StreetType", StreetType.SelectedItem.Text)
                    myCommand.Parameters.AddWithValue("@Suburb", Suburb.Text)
                    myCommand.Parameters.AddWithValue("@PostCode", Postcode.Text)
                    myCommand.Parameters.AddWithValue("@State", State.SelectedItem.Text)
                    myCommand.Parameters.AddWithValue("@Country", Country.SelectedItem.Text)

                    myCommand.ExecuteNonQuery()
                    myConnection.Close()
                End Using

I’ve now changed to C#, and am having problems altering this code. So far I have:

String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

String insertSql = "INSERT INTO tbl_UserProfiles VALUES(@UserID, @FirstName, @LastName, @YearOfBirth, @Country)";
SqlCommand myCommand = new SqlCommand(insertSql, connectionString);
myCommand.Parameters.AddWithValue("@UserID", newUserGuid);
myCommand.Parameters.AddWithValue("@FirstName", FirstNameTB.Text);
myCommand.Parameters.AddWithValue("@LastName", LastNameTB.Text);
myCommand.Parameters.AddWithValue("@YearOfBirth", YearDDL.SelectedItem.Text);
myCommand.Parameters.AddWithValue("@Country", CountryDDL.SelectedItem.Text);

try
{
    connectionString.Open();
    myCommand.ExecuteNonQuery();
}

finally
{
    connectionString.Close();
}

Which I’ve tried to create after looking at a few tutorial sites and my own previous code. But, I believe I’m doing something wrong here:

String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

as I get the squiggly red underline.

  • 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-06-09T21:30:23+00:00Added an answer on June 9, 2026 at 9:30 pm

    It looks like you are trying to Open() a connection string 🙂

    Translate

      Using myConnection As New SqlConnection(connectionString)
                        myConnection.Open()
          Dim myCommand As New SqlCommand(insertSql, myConnection)
    

    To

      using (var myConnection = new SqlConnection(connectionString))
      using (var myCommand = new SqlCommand(insertSql, myConnection))
      {
          myConnection.Open();
          ...
          myCommand.ExecuteNonQuery();
      }
    

    using in C# on your SqlConnection and SqlCommand will guarantee that Dispose() is called on both objects, irrespective of whether success or fail (and close connections, cleanup etc)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a website using Asp.Net MVC which uses a SQL CE database
I have developed an ASP.Net (VB.Net) website which is using Entity Framework and SQL
I have an asp.net website in my production server which running fine now. Now
I have a server which already host ASP.Net website. I am migrating from blogger
I have a website project (C#/ASP.NET) opened in Visual Studio 11 (beta) which works
I'm currently writing a website in ASP.NET MVC, and my database (which doesn't have
I have an ASP.Net website which uses a MySQL database for the back end.
I have asp.net 4 webforms website which references PortableLibrary project. In the website I
I have an ASP.NET website (c# as code behind) in which one of my
I have a web site solution (asp.net) that runs on a SQL server database.

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.