WebForm Insert Using Textbox

I created a simple web form with one text box, a label and a button.  When I go to debug the web form, I receive an error about not being able to connect.

 I listed the relevant code below (I think something is wrong with the button click code).  What changes do you suggest that I make to get this to work?  

Under Button Click
string DescriptionValue = TextBox1.Text;
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("Data Source=ConnectionString");

System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand();
cmd2.Connection = sqlConnection1;
cmd2.Connection.Open();
cmd2.CommandType = System.Data.CommandType.Text;
cmd2.CommandText = "insert into tablename select DescriptionValue";

cmd2.ExecuteNonQuery();
sqlConnection1.Close();

Web Config Connection Strings
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=ServerName;Initial Catalog=databaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>
LVL 1
jjrr007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
Try changing
System.Data.SqlClient.SqlConnection("Data Source=ConnectionString");

Open in new window



to
System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjrr007Author Commented:
I tried that and generated the following error (using System.Data and using System.Data.SqlClient is declared).

Error
System.Data.SqlClient.SqlConnection is a type which is not a valid in this given context

 I tried the following code and generated an error:
string DescriptionValue = TextBox1.Text;
System.Data.SqlClient.SqlConnection sqlConnection3 = new System.Data.SqlClient.SqlConnection("Data Source=ConnectionString");
                sqlConnection3.Open();

                System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand();
                cmd2.CommandType = System.Data.CommandType.Text;
                cmd2.CommandText = "INSERT schema.tablename (ColumnName) VALUES ('DescriptionValue')";
                cmd2.Connection = sqlConnection3;

                cmd2.ExecuteNonQuery();
                sqlConnection3.Close();
0
jjrr007Author Commented:
To keep things simple, below is the code that I'm using.  Do you see anything wrong with it?
string DescriptionValue = TextBox1.Text;
System.Data.SqlClient.SqlConnection sqlConnection3 = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
sqlConnection3.Open();
System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand();
cmd2.CommandType = System.Data.CommandType.Text;
cmd2.CommandText = "INSERT tablename (ColumnName) VALUES ('DescriptionValue')";
cmd2.Connection = sqlConnection3;
cmd2.ExecuteNonQuery();
sqlConnection3.Close();

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jjrr007Author Commented:
The connection string I mentioned on line 2,  in the code above, doesn't have the connection string, but references the connection string name in the web.config file.  

Should I put in the actual connection string on line 2 or just the name that I am using web.config like now?  Are there any other changes suggested?
0
Shaun KlineLead Software EngineerCommented:
I do not see anything wrong with the code. When you use the code above, you are still getting an error on the SQLConnection line?

Is the connection string configuration section you posted exactly as it is in your web.config file, or did you generalize it by removing server name information (which you should do when posting to EE).
0
jjrr007Author Commented:
Thanks for the reply.  I generalized the server name, database name and table name to keep it confidential on EE.  Everything else is as is. I was getting the error message at this line.

sqlConnection3.Open();

Open in new window


Should I put in the actual connection string on line 2 or just the name that I am using web.config?
0
Shaun KlineLead Software EngineerCommented:
To test, you can put the actual string in. However, in production, you should have the connection string in the web.config as it allows for easier modifications.

If you are getting an error on the Open statement, it sounds like there is an issue with the connection string itself. You are using Integrated Security, which means you're web site will need to run under a domain account that has access to your database. Have you set up your Application Pool using a domain account?
0
jjrr007Author Commented:
Thanks for your time and expertise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.