Bader
Bader

Reputation: 207

problem with inserting from textbox to sql

here is my code

SqlCommand insert_user = new SqlCommand("insert into dbo.users (username,password,firstname,lastname,address,country,city,phonenumber,gender,email) VALUES (' bader','123','beno','venp','33','pal','d',''1234','male'," + @TextBox3.Text + ");", badersql);

what i am trying is to insert from textbox3.text to my sql email column , the problem is when i try that , this error msg during debuging popups " Error 9 The name 'TextBox3' does not exist in the current context".

i tried '" + TextBox3.text + "' with @ and without , same problem

if it helps , here is all my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        SqlConnection badersql = new SqlConnection("Data Source=BADER-VAIO\\SQLEXPRESS;Initial Catalog=webage;Persist Security Info=True;User ID=sa;Password=123");

        badersql.Open();
        SqlCommand insert_user = new SqlCommand("insert into dbo.users (username,password,firstname,lastname,address,country,city,phonenumber,gender,email) VALUES (' bader','123','beno','venp','33','pal','d',''1234','male'," + @TextBox3.Text + ");", badersql);

        insert_user.ExecuteNonQuery();
        //insert into webpage.dbo.users (username,password,firstname,lastname,address,country,city,phonenumber,gender,email) VALUES (' bader','123','beno','venp','33','pal','d',''1234','male','[email protected]');
        badersql.Close();


    }


    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {


    }
}

any suggestions ?

regards,

Upvotes: 0

Views: 453

Answers (3)

Bader
Bader

Reputation: 207

it works , i was trying to debug the .cs page itself , thats why the erorr pop ups , thanks everyone for your help

Upvotes: 0

Tim
Tim

Reputation: 5421

Is that a typo in your question or in your code? ''1234' ?

Upvotes: 0

Oded
Oded

Reputation: 498904

You don't have a server side control with the ID TextBox3 on your .aspx page. Did you rename it or remove it?

As an aside - this is wide open to SQL Injection attacks - you should be using parameterized queries instead of string concatenation.

Upvotes: 2

Related Questions