Solved

Caling a subrouting in asp.net c#

Posted on 2010-09-15
11
359 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:AndyPandy
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 14

Expert Comment

by:Daniel Junges
Comment Utility
you have to pass some parameters, you can use the follow:
 AppendSQL(null, null);
0
 
LVL 9

Expert Comment

by:wellhole
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:Daniel Junges
Comment Utility
you can pass null values as parameter because it not are used inside the function
0
 

Author Comment

by:AndyPandy
Comment Utility
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"?

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
Comment Utility
AppendSql appears to be intended to be used as an event handler. The reason you can't get at "gogetit" is because it is scoped to the method where it is defined. If you want it to be available to the method you either need to scope it at the class level, or alter the signature of the AppendSql() method to accept an extra parameter.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 14

Expert Comment

by:Daniel Junges
Comment Utility
enter in Debug mode an analyse the callstack to see where the variable has been setted
0
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
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

0
 
LVL 9

Accepted Solution

by:
puru1981 earned 300 total points
Comment Utility
Two changes
1. declare variable gogetit  Class level
2. AppendSQL(null, null);

string gogetit= string.Empty;
protected void googButton1_Click(object sender, ImageClickEventArgs e)
    {
        gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
        AppendSQL(null,null);
        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

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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.
0
 

Author Closing Comment

by:AndyPandy
Comment Utility
The 'trick' I missed was to declare the variable in the
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now