0

I have 3 textboxes to add Skills, that goes into one column called 'SkillName'. However, I'm getting this error.

'System.Web.UI.Control' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?)

But I have used the assembly using System.Web.UI.WebControls;

This is my code to add textboxes-

 public void InsertSkillInfo()
        {


            String str = @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=DBNAME;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);

            try
            {

                for (int i = 1; i <= 3; i++)
                {
                    conn.Open();
                    **string skill = (Page.FindControl("TextBox" + i.ToString())).Text;**
                    const string sqlStatement = "INSERT INTO Cert (SkillName) VALUES (@SkillName)";
                    SqlCommand cmd = new SqlCommand(sqlStatement, conn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters["@SkillName"].Value = skill;
                    cmd.ExecuteNonQuery();
                }
            }

            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }

            finally
            {
                conn.Close();
            }

         } 

3 Answers 3

2

Page.FindControl will return a Control, but you want a textbox. If you are sure that the control it finds will always be a textbox, then cast it to a textbox.

Either:

string skill = (TextBox)((Page.FindControl("TextBox" + i.ToString()))).Text;

or

var skill = "";
var control = Page.FindControl("TextBox" + i.ToString()) as TextBox;
if(control != null {
    skill = control.Text;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. But now its showing error near Parameter- An SqlParameter with ParameterName '@SkillName' is not contained by this SqlParameterCollection.
Try adding your parameter with something like this: cmd.Parameters.AddWithValue("@SkillName", skill);
0

You need to cast the control to a TexBox so it should be this

string skill = ((TextBox) Page.FindControl("TextBox" + i.ToString())).Text;

Comments

0

You can Try it simply like this

string skill = ((TextBox)(Page.FindControl("TextBox" + i.ToString()))).Text;

2 Comments

Thanks.. But now its showing error near Parameter- An SqlParameter with ParameterName '@SkillName' is not contained by this SqlParameterCollection
That is VERY much open to SQL injection attacks. What would happen if someone entered a skill called ') go drop table Cert go --?

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.