'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?)
The result of FindControl is a Control. Control does not have a Text property. This particular Control is a TextBox, that does have that property. Cast to that, and you will be able to access the Text property just fine.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
girizh
Member
302 Points
404 Posts
Insert multiple textbox values into sql database
Jul 24, 2012 08:32 AM|LINK
Hello all,
I have a textbox: SkillName that will go into the table dbo.Skills(SkillID (AutoIncrement), SkillName)
But the problem is I have Multiple Textboxes and inserting method is different, that's what I learned with this thread.
But I'm getting error. Here is my .cs code.
private void InsertRecords(StringCollection sc) { SqlConnection conn = new SqlConnection(@"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"); StringBuilder sb = new StringBuilder(string.Empty); string[] splitItems = null; foreach (string item in sc) { const string sqlStatement = "INSERT INTO Cert (SkillName) VALUES (@SkillName)"; if (item.Contains(",")) { splitItems = item.Split(",".ToCharArray()); sb.AppendFormat("{0}('{1}'); ", sqlStatement, splitItems[0]); } } try { conn.Open(); SqlCommand cmd = new SqlCommand(sb.ToString(), conn); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } protected void Button1_Click(object sender, EventArgs e) { //int numOfTextBox = 5; StringCollection sc = new StringCollection(); for (int i = 1; i <= 3; i++) { //extract the TextBox values TextBox textbox1 =(TextBox)Page.FindControl("Textbox1" + i); TextBox textbox2 = (TextBox)Page.FindControl("Textbox2" + i); //get the values here and add it in a collection sc.Add(textbox1.Text + "," + textbox2.Text); } InsertRecords(sc); }I'm not understanding this concept, but let me get it right. Let me know where I'm wrong.
Thank you,
Mark As Answer if it helps you :)
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Insert multiple textbox values into sql database
Jul 24, 2012 08:52 AM|LINK
are these multiple textboxes are serverside textboxes or client side ?
if server side then simple place it in a panel and then
foreach(var cntrl in myPanel.Controls)
{
var txtBox = cntrl as TextBox;
if(txtBox!=null)
{
//insert here
}
if these textboxes are client side then loop throug Request.Form the variable names
}
girizh
Member
302 Points
404 Posts
Re: Insert multiple textbox values into sql database
Jul 24, 2012 11:13 AM|LINK
Hello Mudasir,
<asp:TextBox ID="Skill1" class="box" runat="server"></asp:TextBox>
<asp:TextBox ID="Skill2" class="box" runat="server"></asp:TextBox>
.
.
So this is serverside control.
Where do I put the <panel> ? Could you please be more specific.
Many Thanks,
Mark As Answer if it helps you :)
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Insert multiple textbox values into sql database
Jul 25, 2012 08:41 AM|LINK
Hi,
Your situation is different from the one in the link. You can only save one skill as a record in table. Change the code to something like this:
SqlConnection conn = new SqlConnection(@"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
conn.Open();
for (int i = 1; i <= 3; i++)
{
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();
}
conn.Close();
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
vinz
All-Star
127011 Points
17934 Posts
MVP
Re: Insert multiple textbox values into sql database
Jul 25, 2012 09:38 AM|LINK
You sql statement is incorrect. If these textbox are not dynamically created then you can do something like this:
private void InsertRecords(StringCollection sc){ SqlConnection conn = new SqlConnection(@"Data Source=USER-PC\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"); StringBuilder sb = new StringBuilder(string.Empty); foreach (string item in sc) { const string sqlStatement = "INSERT INTO Cert (SkillName) VALUES"; sb.AppendFormat("{0}('{1}'); ", sqlStatement, item); } try { conn.Open(); SqlCommand cmd = new SqlCommand(sb.ToString(), conn); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } protected void Button1_Click(object sender, EventArgs e) { StringCollection sc = new StringCollection(); for (int i = 1; i <= 3; i++) { //extract the TextBox values TextBox txtSkill =(TextBox)Page.FindControl("Skill" + i); sc.Add(txtSkill.Text); } InsertRecords(sc); }PS: Never tested on that, but I think that should help you to get started with.
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
girizh
Member
302 Points
404 Posts
Re: Insert multiple textbox values into sql database
Jul 26, 2012 09:07 AM|LINK
Hello,
For this particular line, I am getting error-
But I have used using System.Web.UI.WebControls;
Mark As Answer if it helps you :)
superguppie
All-Star
48225 Points
8679 Posts
Re: Insert multiple textbox values into sql database
Jul 27, 2012 12:17 PM|LINK
The result of FindControl is a Control. Control does not have a Text property. This particular Control is a TextBox, that does have that property. Cast to that, and you will be able to access the Text property just fine.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Insert multiple textbox values into sql database
Jul 30, 2012 02:13 AM|LINK
Hi,
Sorry, it should be:
string skill = ((TextBox)Page.FindControl("TextBox" + i.ToString())).Text;
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework