?
Solved

Caling a subrouting in asp.net c#

Posted on 2010-09-15
11
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 800 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
 
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 1200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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