Link to home
Start Free TrialLog in
Avatar of jjrr007
jjrr007

asked on

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>
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjrr007
jjrr007

ASKER

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();
Avatar of jjrr007

ASKER

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

Avatar of jjrr007

ASKER

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?
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).
Avatar of jjrr007

ASKER

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?
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?
Avatar of jjrr007

ASKER

Thanks for your time and expertise