jackncoke
jackncoke

Reputation: 2020

Taking 2 textbox values Insert into SQL Server database

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.

Upvotes: 0

Views: 3806

Answers (4)

Tushar Pawar
Tushar Pawar

Reputation: 1

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

Upvotes: 0

Bridge
Bridge

Reputation: 30651

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.

Upvotes: 4

tranceporter
tranceporter

Reputation: 2261

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();

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

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

Upvotes: 2

Related Questions