0

I am trying to learn how to take control values and insert them into a db table. For example I have a textbox called NameBox. I want to grab the text from that textbox and add it into a database table.

I am not trying to do it the best way but the simplest so I can wrap by head around the concept. So far I have:

<asp:TextBox ID="NameBox" runat="server"></asp:TextBox>
<asp:TextBox ID="EmailBox" runat="server"></asp:TextBox>

Here is the code-behind.

protected void Button1_Click(object sender, EventArgs e)
{
    string name1 = NameBox.Text;
    string email1 = EmailBox.Text;

    System.Data.SqlClient.SqlConnection sqlConnection1 =
   new System.Data.SqlClient.SqlConnection(@"Data Source=E:\*********.com1.5\**********.com\App_Data\Home.sdf;");

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT INTO CustomerList (Name, Email) VALUES (" + name1 + "," + email1 + ")";
    cmd.Connection = sqlConnection1;

    sqlConnection1.Open();
    cmd.ExecuteNonQuery();
    sqlConnection1.Close();
}

Error message I am getting is

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I googled the error message and a lot of what i been reading is saying my connection is probably wrong. I tried testing this by using some of the drag and drop controls and I have a repeater working with the same connection string.

Right now I don't even really know if this is right approach. As I am sure you can all relate at one point or another I want to see it work on a very simple level so i can start to play with it and see how it all really works.

4
  • 4
    Two words: Parametrized queries Commented Aug 31, 2012 at 15:33
  • As the reading you found suggests, it's having trouble connecting, so it's either your connection string, or some environmental factor that's keeping you from accessing it. And for the love of pete...parameterize! Commented Aug 31, 2012 at 15:33
  • 2
    But from there the SQL query is incorrect; the strings aren't surrounded by apostrophes, and are vulnerable to injection attacks even if they were. See: codinghorror.com/blog/2005/04/… Commented Aug 31, 2012 at 15:34
  • Your connection string is wrong. You tagged this as SQL server, so you need to connect using the servername\instancename, not a lettered drive. Your accepted answer has nothing to do with your original error. Commented Jan 25, 2014 at 4:35

4 Answers 4

4

Your connection string infers you're using SQL Server Compact Edition - in which case you want to use SQLCeCommand and SQLCeConnection instead. Please also look into using parameterised queries instead of concatenating strings together - it's better for performance and security reasons. Please read this link for more background on SQL injection - at the moment you're vulnerable to it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i am aware of that. This isn't going to see run-time. I am trying to teach my self. Thank you for the feedback i want to look into this and read you recommended links.
2

You must use SqlCeConnection in order to create connection

link : http://msdn.microsoft.com/fr-fr/library/system.data.sqlserverce.sqlceconnection%28v=vs.80%29.aspx

Comments

0

Just copy paste the connection string you are using for the repeater code, and paste it in the Button1_Click code, where you create a new connection. There might be an extra space, or some capital letter mismatch. And for insertions, I would create a stored procedure in the database, which takes Name and Email parameters, and use the SqlCommand to execute the stored procedure.

CREATE PROCEDURE INSERT_NAME_EMAIL_DATA
    @NAME VARCHAR(100) NULL,
    @EMAIL VARCHAR(100) NULL
AS
BEGIN
      INSERT INTO CustomerList (Name, Email) VALUES (@NAME, @EMAIL)
END  

And in your code you can execute the stored procedure by doing:

cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(
    new SqlParameter("@NAME", name1));
cmd.Parameters.Add(
    new SqlParameter("@EMAIL", email1));
cmd.Connection = sqlConnection1;

        sqlConnection1.Open();
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();

1 Comment

Wasn't me unless i did it on accident.
0

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

For above Error, you have restart your sqlserver instance by going to window of services as following

Press windows + r

type services.msc + enter

find your sqlserver instance and restart it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.