Reputation: 11
I'm new to ASP.NET and have been stuck with this problem for two weeks. Please help!
I'm trying to build a website with searching functions to my local database built using MySql. I want the search results to show on a separated page for my website. I used the server transfer method, and have my code as the following:
For the page to enter search text, I have a text box and a button for click and the .cs file is:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
public partial class Default_Page : System.Web.UI.Page
{
public string Default_Page_TextBox1
{
get { return Default_Page_TextBox1.Text; }
set { this.Default_Page_TextBox1.Text=value; }
}
private void Default_Page_Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("Default_Search_Result.aspx");
}
}
For the page to display search result, the .cs file is:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
public partial class Default_Search_Result : System.Web.UI.Page
{
MySqlConnection connection = null;
MySqlCommand command = null;
MySqlDataReader reader = null;
protected void Page_Load(object sender, EventArgs e)
{
string myConString = "SERVER=localhost;" +
"DATABASE=mydb;" +
"UID=**;" +
"PASSWORD=**;";
connection = new MySqlConnection(myConString);
Default_Page Dp;
Dp = (Default_Page)Context.Handler;
command = connection.CreateCommand();
command.CommandText = "select BkName from bkinfo where bkname = '" + Default_Page_TextBox1.Text + "'";
connection.Open();
reader = command.ExecuteReader();
if (reader.Read())
{
Label1.Text = reader.GetValue(0).ToString();
}
else
{
Label1.Text = "Not Found";
}
connection.Close();
}
}
The searching doesn't work. Visual Studio captures 3 errors:
on the page to enter text for searching (Default_Page), the
return Default_Page_TextBox1.Text;
shows:
'string' does not contain a definition or 'Text' and no extension method 'Text' accepting a first argument of type 'string' could be found.
on the page display search result (Default_Search_Result), the
Default_Page Dp;
shows:
the type or namespace name 'Default_Page' could not be found
still on the page display search result (Default_Search_Result), the
command.CommandText = "select BkName from bkinfo where bkname = '" + Default_Page_TextBox1.Text + "'";
shows:
the name 'Default_Page_TextBox1" does not exist in the current context.
Could someone help me figure out what is wrong please?
Upvotes: 1
Views: 843
Reputation: 91666
There's quite a bit wrong with this code, which makes me think that you might want to step back and start with some simpler C# tutorials. However, maybe I can point you in the right direction as to why each error might be occuring. Let's start with:
public string Default_Page_TextBox1
{
get { return Default_Page_TextBox1.Text; }
set { this.Default_Page_TextBox1.Text=value; }
}
You've implemented a property that refers to itself. In other words, Default_Page_TextBox1
is a string, whose getter returns a property of itself. The compiler error is happening as string
does not have a property called Text
, however that's the least of your problems. You'll end up calling the Default_Page_TextBox1
getter recursively, and end up getting a stack overflow. Perhaps you have a web control called TextBox1
, in which case you can just refer to that directly (as the Visual Studio designer will implement that in your partial class.
Next is the error *the type or namespace name 'Default_Page' could not be found*. Obviously, you've declared a class called Default_Page
. I'll assume it's in the same assembly, so that's not the problem. However, since there is a compiler error (see above) with this class, I'd guess the type isn't found because that class was never correctly compiled. Fix the error above and this error should go away.
Lastly is:
command.CommandText = "select BkName from bkinfo where bkname = '" + Default_Page_TextBox1.Text + "'";
There's a few issues with this statement. First, the class Default_Search_Result
does not have a property called Default_Page_TextBox1
. You seem to think you can access your property in your other class after you've Server.Transfer()
'ed away from it. This is wrong.
A better solution would be to implement your <form>
to simply POST to Default_Search_Result
directly, and read the search query using Request.QueryString
instead.
Also, keep in mind building a SQL select statement using string concatenation is a bad idea. If your text contains an apostrophe, it will end the string constant. This can be taken advantage of to run arbitrary SQL statements, which is a security flaw known as SQL injection. Your code is fine if you're just learning, but never ever do this on any code that matters. Go read up on how to parameterize SQL queries to fix this.
Upvotes: 1