Solved

Caling a subrouting in asp.net c#

Posted on 2010-09-15
11
360 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
ID: 33684383
you have to pass some parameters, you can use the follow:
 AppendSQL(null, null);
0
 
LVL 9

Expert Comment

by:wellhole
ID: 33684385
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
ID: 33684397
you can pass null values as parameter because it not are used inside the function
0
 

Author Comment

by:AndyPandy
ID: 33684440
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
ID: 33684480
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

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

Expert Comment

by:mrichmon
ID: 33684793
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
ID: 33685108
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
ID: 33685578
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
ID: 33686126
The 'trick' I missed was to declare the variable in the
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33686528
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

14 Experts available now in Live!

Get 1:1 Help Now