AndyPandy
asked on
Caling a subrouting in asp.net c#
Experts,
Newbie question.
I want to call a subroutine in a asp.net c# webform.
I get a error saying:
"Compiler Error Message: CS1501: No overload for method 'AppendSQL' takes 0 arguments"
See code below.
Thanks
Newbie question.
I want to call a subroutine in a asp.net c# webform.
I get a error saying:
"Compiler Error Message: CS1501: No overload for method 'AppendSQL' takes 0 arguments"
See code below.
Thanks
protected void googButton1_Click(object sender, ImageClickEventArgs e)
{
string gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
AppendSQL();
Response.Redirect(gogetit);
}
protected void AppendSQL(object s, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["CS1"].ToString();
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
using (SqlConnection con = new SqlConnection(strConn))
{
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand(@"INSERT INTO gogetit (term,url,IP,createdate) Values ('"
+ TextBox1.Text + "','"
+ gogetit + "','"
+ clientIPAddress
+ "', getdate())");
// 2. Set the Connection property
cmd.Connection = con;
// 3. Open connection
if (con.State == ConnectionState.Closed)
{
con.Open();
}
try
{
// 4. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
catch (SqlException se)
{
Response.Write("Result: " + se.Message);
}
cmd.Dispose();
con.Close();
}
}
Your subroutine requires 2 parameters as shown in your function declaration "protected void AppendSQL(object s, EventArgs e)". However, your call to this in googButton1_Click does not pass it any parameters.
you can pass null values as parameter because it not are used inside the function
ASKER
I passed it two parameters (null,null), but now it can't find the variable "gogetit" that was set in the calling routine.
What do I do to have AppendSQL to see the variable "gogetit"?
What do I do to have AppendSQL to see the variable "gogetit"?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
enter in Debug mode an analyse the callstack to see where the variable has been setted
Here is the code for the solution mentioned by carl_tawn:
protected void googButton1_Click(object sender, ImageClickEventArgs e)
{
string gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
AppendSQL(null, null, gogetit);
Response.Redirect(gogetit);
}
protected void AppendSQL(object s, EventArgs e, string gogetit)
{
string strConn = ConfigurationManager.ConnectionStrings["CS1"].ToString();
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
using (SqlConnection con = new SqlConnection(strConn))
{
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand(@"INSERT INTO gogetit (term,url,IP,createdate) Values ('"
+ TextBox1.Text + "','"
+ gogetit + "','"
+ clientIPAddress
+ "', getdate())");
// 2. Set the Connection property
cmd.Connection = con;
// 3. Open connection
if (con.State == ConnectionState.Closed)
{
con.Open();
}
try
{
// 4. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
catch (SqlException se)
{
Response.Write("Result: " + se.Message);
}
cmd.Dispose();
con.Close();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Although, mrichmons code will do the, it will cause you a problem if AppendSql() is actually being used as an event handler as it will no longer match the required signature for the EventHandler delegate, in which case you'll just have to increase the scope of the "gogetit" variable.
ASKER
The 'trick' I missed was to declare the variable in the
You're webform is a class that derives (inherits in OO speak) from the Page class. AppendSql() is referred to as a "method" of the class.
AppendSQL(null, null);