none
Problems inserting in database RRS feed

  • Question

  • Hi,

    Sorry for the newbie question.

    I'm trying to get the textbox with ID=tName value inserted in a database.

    I get the error:
    "Connection Error: SQL code: The name "tName.Text" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted."

    Here's my code:
    ---------------------------------------------------------------------------------
    protected void tButton_Click(object sender, EventArgs e)
        {
            {

                // Connect to MS SQL SERVER
                SqlConnection MyConnection = new SqlConnection("Server=192.168.1.21;" + "DataBase=vpopmail;" + "User ID=sa;" + "Password=secret");
               
               
                try
                {
                    string name;
                    name = tName.Text = Server.HtmlEncode(tName.Text);
                    MyConnection.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO vpopmail (pw_name, pw_domain) VALUES (tName.Text, 'wwlib.lan')", MyConnection);
                    cmd.ExecuteNonQuery();
    ---------------------------------------------------------------------------------

    Any help would be appreciated.

    Warm Regards,
    Mário Gamito
    Monday, February 18, 2008 10:23 AM

All replies

  • If you want to insert text from textbox tName then you will have to build dynamic text for your sql command. It won't be replaced by the actual text in the code you have written because you have hardcoded literal "tName.Text" as a value of pw_name.
    Monday, February 18, 2008 10:40 AM