简体   繁体   中英

Retrieve single user data from oracle database into visual studio C# application

I am using forms in visual studio to create an application and then retrieve data from Oracle database. My all parts are working except getting data for the single user. Here is form.cs section for that part

private void getCustomerStringToolStripMenuItem_Click(object sender, EventArgs 
e)
    {
        Getcuststring g = new Getcuststring();
        g.Show();
    }

This is my partial class named getcuststring.cs

namespace Assignment
{
public partial class Getcuststring : Form
{
    public Getcuststring()
    {
        InitializeComponent();
    }

    private void Getcuststring_Load(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Getting();
    }

    public void Getting()
    {
        OracleConnection conn = new OracleConnection();


        conn.ConnectionString = "Data Source=(DESCRIPTION =" + "(ADDRESS = (PROTOCOL = TCP)" +
            "(HOST = *******)(PORT = 1521))" + "(CONNECT_DATA =" + "(SID = dms)));"
            + "User Id= *****;Password= ******;";

        conn.Open();


        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;

        cmd.CommandText = "GET_CUST_STRING_FROM_DB";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("pcustid", OracleDbType.Long).Value = textBox1.Text;
        cmd.Parameters.Add("return_value", OracleDbType.Varchar2).Direction = ParameterDirection.ReturnValue;
        try
        {

            cmd.ExecuteNonQuery();
         //   string result = string.Empty;
            var result = cmd.Parameters["result_value"].Value.ToString();
            MessageBox.Show(result);
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
        finally
        {
           conn.Close();
        }

    }
}

my all other functions are working but when executing this in visual studio it every time give exception out saying no data is found whereas that customer id is present in the Oracle database but it somehow not showing the result but instead raising the exception. So can u tell me where I am wrong, why he's providing me with an exception saying no data found whereas that data actually exist.

This is the function

CREATE OR REPLACE FUNCTION GET_CUST_STRING_FROM_DB(pcustid NUMBER) RETURN 
VARCHAR2 AS 
vcustid NUMBER;
vcustname VARCHAR2(255);
vcustsales NUMBER;
vcuststatus VARCHAR2(255);
BEGIN
SELECT CUSTID,CUSTNAME,SALES_YTD,STATUS INTO 
vcustid,vcustname,vcustsales,vcuststatus
FROM CUSTOMER
WHERE CUSTID = pcustid;

RETURN 'CustID: ' || vcustid || ' Name: ' || vcustname || ' Status: ' || 
vcuststatus || ' SalesYTD: ' || vcustsales; 

EXCEPTION
WHEN NO_DATA_FOUND THEN
    RAISE_APPLICATION_ERROR(-20021,'Customer ID not found');
WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR(-20000,SQLERRM);
END;

I think it is simple typo, line

var result = cmd.Parameters["result_value"].Value.ToString();

should be:

var result = cmd.Parameters["return_value"].Value.ToString();

I built similiar case, and got System.IndexOutOfRangeException when I run it. Correcting typo solved problem. Whole code which worked for me:

OracleConnection CONNECTION 
    = new OracleConnection("Data Source=XX;User Id=XX;Password=XX;");
CONNECTION.Open();
OracleCommand cmd = new OracleCommand() { Connection = CONNECTION };
cmd.CommandText = "GET_CUST_STRING_FROM_DB";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("pcustid", OracleType.Double).Value = textBox1.Text;
cmd.Parameters.Add("return_value", OracleType.VarChar, 500).Direction 
    = System.Data.ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
var result = cmd.Parameters["return_value"].Value.ToString();

Console.WriteLine(result);

CONNECTION.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM