Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot convert to string 2

Status
Not open for further replies.

CRuser89

Programmer
May 18, 2005
79
US
Hello everyone,

I am using VS2005 (C#) and have the following code behind for my login page. When I try to compile it, I get the following error on the line in bold below. Seems like I need to convert "UserName" and "Password" to strings. Can someone please help me with this?

Thanks,
Kathy

error
Argument'1': cannot convert from 'System.Web.UI.WebControls.TextBox' to 'string'




code behind
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using LoginInfoTableAdapters;


namespace LoginTest
{

public partial class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.RequiredFieldValidator rvUserValidator;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.RequiredFieldValidator rvPasswordValidator;
protected System.Web.UI.WebControls.Button cmdSubmit;
protected System.Web.UI.WebControls.ValidationSummary Validationsummary1;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Label lblMessage2;

private void Page_Load(object sender, System.EventArgs e)
{

}

protected void cmdSubmit_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
tblPeopleTableAdapter LoginAdapter = new tblPeopleTableAdapter();

LoginInfo.tblPeopleDataTable Criteria =
LoginAdapter.GetDataBy(UserName, Password);

this.GridView1.DataSource = Accounts;
this.GridView1.DataBind();

}

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}

 
The GetDataBy is something yours, specified in the adapter... i think. You need to pass the texts of the textboxes:

LoginInfo.tblPeopleDataTable Criteria = LoginAdapter.GetDataBy(UserName.Text, Password.Text);

I am not sure if it works. Try also (for both controls. I show you for one):

- this.UserName.Text
- ((TextBox)this.FindControl("UserName")).Text


Hope this helps
 
Thank you tipgiver for your tips. I tried passing the texts of the textboxes but that still didn't work. I'm getting the error - "Object reference not set to an instance of an object".

I am very new to .net and didn't understand how to try out your second tip.

Thanks again,
Kathy
 
Firstly, you need to pass the Text property of the TextBox in order to get it's value in a String format. Secondly, it depends where your TextBox is located. You will have to paste the HTML portion of your page in order for us to help you with this.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Tipgiver - .GetDataBy is a method that was created through the TableAdapter Query Configuration Wizard. I created a query to pull all records of the people table based on the Username and Password parameters provided. When I plug in actual fields like "abc", "password" (these are actual records in the people table, then it works fine. But it doesn't like it when I pass the parameters.

ca8msm - I have included the login.aspx code below. Thank you both for your assistance.

login.aspx

<%@ Page language="C#" CodeFile="Login.aspx.cs" AutoEventWireup="false" Inherits="LoginTest.WebForm1"%>

<html xmlns=" <head>
<title>WebForm1</title>
<link href="../StyleSheet.css" type="text/css" rel="stylesheet" />
</head>
<body>

<form id="frmlogin" method="post" runat="server">
<asp:Login ID="Login1" align="center" runat="server" Width="247px">
<LayoutTemplate>
<table width="100%">
<tr>
<td class="header1" align="right" style="width: 90px">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" Font-Bold="False">UserName:</asp:Label>
</td>
<td style="width: 146px" align="right">
<asp:TextBox ID="UserName" runat="server" Width="121px"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="UserName is required." ToolTip="Password is required." ValidationGroup="Login1" Width="14px">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="header1"align="right" style="width: 90px">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Font-Bold="False">Password:</asp:Label>
</td>
<td style="width: 146px" align="right">
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="121px"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1" Width="14px">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: red; height: 11px;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="cmdSubmit" runat="server" Text="Log In" ValidationGroup="Login1" Font-Bold="False" ForeColor="Black" OnClick="cmdSubmit_Click"/>
</td>
</tr>
<tr>
<td class="text2" align="center" colspan="2" >
<asp:HyperLink ID="HyperLink1" runat="server" ForeColor="Black" NavigateUrl="~/Recover.aspx" EnableTheming="True">Request Account</asp:HyperLink>
</td>
</tr>
</table>
<tr class="Err2" valign="top" style="height: 25px" >
<td align="center" colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" align="center" runat="server" ValidationGroup="Login1" DisplayMode="List" />

</td>
</tr>
</LayoutTemplate>
</asp:Login>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
 
OK, as for my second point, in order to get a reference to the TextBox, you need to use the parent container's FindControl method and then convert it to a TextBox. Something like this:
Code:
Ctype(Login1.FindControl("UserName), TextBox).Text


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
In C#: ((TextBox)Login1.FindControl("UserName")).Text();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top