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.SqlC onnection sqlConnection1 = new System.Data.SqlClient.SqlC onnection( "Data Source=ConnectionString");
System.Data.SqlClient.SqlC ommand cmd2 = new System.Data.SqlClient.SqlC ommand();
cmd2.Connection = sqlConnection1;
cmd2.Connection.Open();
cmd2.CommandType = System.Data.CommandType.Te xt;
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;Integ rated Security=True" providerName="System.Data. SqlClient" />
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integr ated Security=SSPI;AttachDBFile name=|Data Directory| \aspnetdb. mdf;User Instance=true" providerName="System.Data. SqlClient" />
</connectionStrings>
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.SqlC
System.Data.SqlClient.SqlC
cmd2.Connection = sqlConnection1;
cmd2.Connection.Open();
cmd2.CommandType = System.Data.CommandType.Te
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;Integ
<add name="ApplicationServices"
</connectionStrings>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
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?
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).
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).
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.
Should I put in the actual connection string on line 2 or just the name that I am using web.config?
sqlConnection3.Open();
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?
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?
ASKER
Thanks for your time and expertise
ASKER
Error
System.Data.SqlClient.SqlC
I tried the following code and generated an error:
string DescriptionValue = TextBox1.Text;
System.Data.SqlClient.SqlC
sqlConnection3.Open();
System.Data.SqlClient.SqlC
cmd2.CommandType = System.Data.CommandType.Te
cmd2.CommandText = "INSERT schema.tablename (ColumnName) VALUES ('DescriptionValue')";
cmd2.Connection = sqlConnection3;
cmd2.ExecuteNonQuery();
sqlConnection3.Close();