Problem inserting text from newly added textbox in register.aspx page into sql server 2008
Hi The problem i'm having is that when i make a new textbox iD="LoginName" i can't refer to it in the code behind. I was able to figure out that using RegisterUser.FindControl("LoginName") adds empty text in the database but it doesn't get the text entered when subscribing.
My code:
System.Data.SqlClient.SqlCommand myCommand =
new System.Data.SqlClient.SqlCommand(
"INSERT INTO tblGebruikers(tLogName, tGebName, tGebPassw, tGebEmail) VALUES('"
+ RegisterUser.FindControl("LoginName")+ "','" + RegisterUser.UserName
+ "','" + RegisterUs开发者_C百科er.Password + "','" + RegisterUser.Email
+ "') ", sqlConn);
aspx code:
<asp:TextBox ID="LoginName" runat="server" CssClass="textEntry"></asp:TextBox>
I work with VS 2010 and SQL Server 2008
Any help on what i'm doing wrong is much appreciated.First in relation to your initial question note that FindControl returns a Control, if you want to see what someone entered into said control you will need to cast it back to a textbox and reference the Text property.
Aside from that building your SQL command in that way leaves you wide open to injection attacks. Consider what would happen if I typed something like this into your textbox:
', '', '', '')GO Drop Table tblGebruikers GO --
to close this ticket.
TextBox txtLogName = (TextBox)this.RegisterUserWizardStep.ContentTemplateContainer.FindControl("LoginName");
is what i was looking for. I had to insert this in the
protected void RegisterUser_CreatedUser(object sender, EventArgs e){}
method and than I was able to write the text entered in the LoginName
textbox to the sql server database.
精彩评论