locked
Problems with database insertion 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:24 AM

All replies

  • Hi,

     

    You have mistakenly taken the tName.Text inside the quotes while creating the SqlCommand. What you want is to include the value of tName.Text while creating the SqlCommand object. Try the following line instead:

     

    SqlCommand cmd = new SqlCommand("INSERT INTO vpopmail (pw_name, pw_domain) VALUES ('" + tName.Text + "', 'wwlib.lan')", MyConnection);

     

    regards

    Sharad

     

    Monday, February 18, 2008 10:58 AM