0

I have an aspx page in that I have a button.

Now what I want it, i want to write a query on the onclick of the button. How to to that. ?

Here is my code:-

<asp:Button ID="btnUpdate" runat="server" Text="Update Broker Rating" 
        Width="145px" onclick="btnUpdate_Click" />

Also see my C# code:-

protected void btnUpdate_Click(object sender, EventArgs e)
{
  // how to write the query
}

UPDATE

I tried like below, but I am getting error as

connection property has not been initialized

Here is my code:-

protected void btnUpdate_Click(object sender, EventArgs e)
{
    //string strConnString = "";
    OracleConnection con = new OracleConnection(ConfigurationManager.AppSettings["OracleConn"]);
    string strQuery = "SELECT ab.broker_id,CASE WHEN SYSDATE - la.creation_date <= 180 THEN 'A' WHEN SYSDATE - cef_dt <= 30 THEN 'B' ELSE 'C'END rating " +
                      "FROM xxacl_pn_new_cha_part ab,xxacl_pn_lease_det ld,xxacl_pn_leases_all la,xxcus.xxacl_pn_customer_enquiry_v ce " +
                      "WHERE ab.broker_id = ld.broker_id AND ld.booking_no = la.booking_no AND ab.broker_id = ce.broker_id";

    OracleCommand cmd = new OracleCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = con;
    try
    {
        con.Open();
        OracleDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            //update message to be displayed here   
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}
Nad
  • 4,605
  • 11
  • 71
  • 160
  • http://someictstuff.blogspot.in/2013/07/aspnet-how-to-execute-query-to-oracle.html – Neel Sep 12 '15 at 06:27
  • @Neel: sir can you post only the relevant info here,as it's in vb and whole connectionstring is mentioned. **I want to keep short and simple** – Nad Sep 12 '15 at 06:29
  • @Neel: Please see my update and help if possible..! – Nad Sep 12 '15 at 06:57
  • check your web.config file for ConnectionString properly coded or not? – Rojalin Sahoo Sep 12 '15 at 07:09
  • @RojalinSahoo: yes it is properly coded..! – Nad Sep 12 '15 at 07:10
  • http://stackoverflow.com/questions/1007786/how-to-fix-the-connectionstring-property-has-not-been-initialized follow this may help you. – Rojalin Sahoo Sep 12 '15 at 07:15
  • @RojalinSahoo: now it went to _finally_ block. what should I do to display succesful message and in which block ?? – Nad Sep 12 '15 at 07:22
  • use break point and check it that it execute while block if yes then use `ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('update done');", true); ` – Rojalin Sahoo Sep 12 '15 at 07:27
  • @RojalinSahoo: its going in finally block – Nad Sep 12 '15 at 07:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89445/discussion-between-rojalin-sahoo-and-nadeem). – Rojalin Sahoo Sep 12 '15 at 07:29

1 Answers1

0

Change this line:

OracleCommand cmd = new OracleCommand();

... to these two lines:

OracleCommand cmd = new OracleCommand(strQuery, conn);

and for your connection string:

var con = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
Neel
  • 11,625
  • 3
  • 43
  • 61
  • getting error as `the best overloaded method has some invalid arguments` – Nad Sep 12 '15 at 07:02
  • still getting error as `The ConnectionString property has not been initialized.` – Nad Sep 12 '15 at 07:06
  • it is because you are not assigning connection string properly – Neel Sep 12 '15 at 07:10
  • any other way to assign it ?? I have used the default way to assign it..! – Nad Sep 12 '15 at 07:11
  • i used like this `OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);` now it didn't gaved me error, it went into _finally_ block. What I want is on succesfull debug i want to display message that it has been succesfully submitted – Nad Sep 12 '15 at 07:14
  • yes, there was some silly mistake related to the DB. – Nad Sep 12 '15 at 08:44