Link to home
Start Free TrialLog in
Avatar of AndyPandy
AndyPandyFlag for United States of America

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
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();
        }
    }

Open in new window

Avatar of Daniel Junges
Daniel Junges
Flag of Brazil image

you have to pass some parameters, you can use the follow:
 AppendSQL(null, null);
Avatar of wellhole
wellhole

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
Avatar of AndyPandy

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"?

SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.