Oracledependency event is not firing - c#

I have some problems using OracleDependency.
I've read docs on msdn and oracle and copy some code to try it.
However this is not working, the event on_my_event doesn't fire when the insert is done.
Does anyone know why ?
My user has CHANGE NOTIFICATION rights on the database. Oracle Server is 11.2.0.3.0.
Here is the code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string connection_string = "Data Source=My_srv;User Id=My_usr;Password=My_pwd;";
OracleConnection connection = null;
OracleDependency dependency = null;
OracleCommand my_select = null;
OracleCommand my_insert = null;
public Form1()
{
InitializeComponent();
}
private void TB_insert_event(object sender, EventArgs e)
{
if (TB_insert.Text == "Name of your insert")
TB_insert.Clear();
}
private void insert(object sender, EventArgs e)
{
connection = new OracleConnection(connection_string);
my_insert = connection.CreateCommand();
my_insert.CommandText = "INSERT INTO USR_DEV_TRUNK.WPARAM (wpa_codeparam) VALUES ('" + TB_insert.Text + "')";
connection.Open();
my_insert.ExecuteNonQuery();
connection.Close();
}
private void Set_dep(object sender, EventArgs e)
{
OracleDependency.Port = 3048;
connection = new OracleConnection(connection_string);
connection.Open();
my_select = connection.CreateCommand();
my_select.CommandText = "SELECT wpa_codeparam FROM USR_DEV_TRUNK.WPARAM";
dependency = new OracleDependency();
dependency.AddCommandDependency(my_select);
my_select.Notification.IsNotifiedOnce = false;
my_select.ExecuteNonQuery();
dependency.OnChange += new OnChangeEventHandler(on_my_event);
TB_dependency.Text = "The dependency is set, do your insert to see if it works";
connection.Close();
}
public void on_my_event(object obj, OracleNotificationEventArgs arg)
{
TB_dependency.Text = "Yay ! It worked !";
}
}
}
I have two buttons :
One to set my dependency (function (on click) : Set_dep)
One to do my insert (function (on click) : insert)
And i have two textboxs :
One to get my insert (name : TB_insert)
One to show the dependency state (name : TB_dependency)

Did you make sure packets on port 3048 are not getting blocked by a firewall?
Also, once your dependency has been set, can you see it by querying the USER_CHANGE_NOTIFICATION_REGS / DBA_CHANGE_NOTIFICATION_REGS views?

Related

Sql data not displaying in Windows Form ListView using C#

I am new to coding and have to complete this task as part of a Multi-Layer application. This code runs but the data does not appear in the required ListView. I am not sure what I have done wrong as I have another ListView that uses similar code and works fine. There are only the two columns CategoryID and Category. Any help would be appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Acme.Data_Access_Layer;
using Acme.Business_Layer;
namespace Acme
{
public partial class frmCategoriesView : Form
{
public frmCategoriesView()
{
InitializeComponent();
}
private void DisplayCategories()
{
string selectQuery = "SELECT Categories.CategoryID, Categories.Category FROM Categories ";
SqlConnection conn = ConnectionManager.DatabaseConnection();
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//Define the list items
Categories categories = new Categories(int.Parse(rdr["CategoryID"].ToString()), rdr["Category"].ToString());
ListViewItem lvi = new ListViewItem(categories.CategoryID.ToString());
lvi.SubItems.Add(categories.Category);
lvCategory.Items.Add(lvi);
}
if (rdr != null)
rdr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unsuccessful" + ex);
}
}
private void frmCategoriesView_FormClosing(object sender, FormClosingEventArgs e)
{
frmMainForm mainForm = new frmMainForm();
mainForm.Show();
this.Hide();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmCategoriesView_Load(object sender, EventArgs e)
{
DisplayCategories();
}
}
}
Change
"SELECT Categories.CategoryID, Categories.Category FROM Categories ";
to
"SELECT CategoryID, Category FROM Categories ";
The issue was the Load Event. I ended up deleting the event code, ensuring that it was cleared in properties and then redid the event and it worked.

error occored The connection was not closed The connection current state is open

My code is like this : I input the caseid in the textbox and try to click Find button to find the corresponding result. But Visual studio always says :error occored The connection was not closed The connection current state is open.
This is my first time design ADO.NET myself and I need to finish my advisor research tasks.
Could you check the code and tell me what wrong?
Thanks
Sophia
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace casestudy
{
public partial class Form1 : Form
{
SqlConnection vcon1 = new SqlConnection(#"Data Source=SOPHIA-PC\SQLEXPRESS;Initial Catalog=casestudy;Integrated Security=True");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
vcon1.Open();
}
catch (Exception ex)
{
MessageBox.Show("error.occured" + ex.Message);
this.Dispose();
}
}
private void Find_Click(object sender, EventArgs e)
{
string querystring = "SELECT * FROM AssignedSolution WHERE CASEID = #caseid";
SqlCommand Vcom = new SqlCommand(querystring, vcon1);
Vcom.Parameters.AddWithValue("#caseid", txtCASEID);
SqlDataReader rdr = null;
try
{
Vcom.Connection = vcon1;
vcon1.Open();
Vcom.ExecuteNonQuery();
//DataSet vds1 = new DataSet();
rdr = Vcom.ExecuteReader();
//vDa1.Fill(vds1, "res");
//dataGridView1.DataSource = vds1.Tables["res"];
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
Vcom.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("error.occured" + ex.Message);
}
finally
{
vcon1.Close();
vcon1.Dispose();
}
}
The connection is being opened on Form1_Load event, then you are trying to open the same connection again in the Find_Click event. You can't open a connection that is already open. Just remove all code from the load event, you don't need it.

Button click isnt getting triggered on hitting the enter key

I am trying to emulate a button click when the enter key is pressed in a text box. I have used this before too but it just doesn't seem to work now.
Please check the code I am using below and let me know if I missed something. I have been away from coding for almost a year now maybe I am missing something?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
String uname, password, query;
int counter = 0;
public Form1()
{
InitializeComponent();
}
private void login_Click(object sender, EventArgs e)
{
verify();
}
private void pass_KeyDown(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
verify();
}
}
private void verify()
{
uname = name.Text; //Initialize variables
password = pass.Text;
uname = uname.Trim(); //Sanitize input
password = password.Trim();
query = "SELECT * FROM login WHERE uname = #uname AND pass = #pass";
string conString = Properties.Settings.Default.libConnectionString;
using (SqlCeConnection conn = new SqlCeConnection(conString))
{
conn.Open();
using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
{
cmd.Parameters.AddWithValue("#uname", uname);
cmd.Parameters.AddWithValue("#pass", password);
cmd.ExecuteNonQuery();
SqlCeDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
counter = 1;
Variables.username = reader["uname"].ToString();
Variables.passw = reader["pass"].ToString();
if (reader["regid"].ToString() != null)
{
Variables.regid = reader["regid"].ToString();
}
MessageBox.Show(Variables.username + ", you are now logged in!");
}
if (counter == 0)
{
MessageBox.Show("Invalid details");
}
}
}
}
}
}
I am coding in C# in the IDE Visual Studio 2012
Whenever you create a method with the intention to handle an event, you need to make sure you register the method as a handler to the desired event. If you are not doing so the program will not guess that your method was meant to be an event handler.
try this :
if (args.KeyCode == Keys.Return)
{
login_Click.PerformClick();
}

How to fix 'System.Data.OleDb.OleDbException'?

first off sorry to ask how to fix this error (i know its a common question) but i am quite new to C# and I cannot seem to find a solution for it.
I am making a windows form that imports data from an excel file and displays it in a DataGridView. When executing I get the error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll Additional information: No value given for
one or more required parameters."
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class CurrentOrders : Form
{
public CurrentOrders()
{
InitializeComponent();
}
private void CurrentOrders_Load(object sender, EventArgs e)
{
}
private void BackBtn_Click(object sender, EventArgs e)
{
NewOrder NewOrd = new NewOrder();
this.Hide();
NewOrd.Show();
}
private void DataGridViewLOG_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Tombies\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\PCSsheet.xls" + #";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
OleDbCommand command = new OleDbCommand
(
"SELECT DATE, CUSTOMER, PO, COMMENTS, PCS FROM [LOG$]", conn
);
DataSet DsOrderLOG = new DataSet();
OleDbDataAdapter Adapter = new OleDbDataAdapter(command);
conn.Open();
Adapter.Fill(DsOrderLOG);
conn.Close();
DataGridViewLOG.DataSource = DsOrderLOG.Tables[0];
}
}
}
I know it has something to do with the 'Adapter.Fill' at the bottom, but from there on I'm lost.
Any help is appreciated!
Date is probably the culprit. Try putting it (an all other column names, for that matter) in brackets:
"SELECT [DATE], [CUSTOMER], [PO], [COMMENTS], [PCS] FROM [LOG$]", conn

Edit database in management studio using visual studio c#

I have created a database using SQL Server Management Studio and am now trying to edit that database using Visual Studio Express 2012. I have connected the database to Visual Studio and can see my database but I have not been able to edit the database stored in Management Studio using Visual Studio. I have created a form and am trying to insert what is entered into textbox1 into a specific cell on my database after the user defines the column name and row (using the primary key in my DB) with textbox2 and textbox3. What code can I use to perform this action? So far I have had no luck. Thank you in advance for you help.
This is my current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using Microsoft.SqlServer.Server;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection myConnectionString = new SqlConnection("Data Source=Server Catalog=DataBase;Integrated Security=True");
SqlCommand command = new SqlCommand();
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection dbConnection = new SqlConnection()) // the "using" construct will close and dispose of the connection
{
dbConnection.ConnectionString = ("Data Source=Server ;Initial Catalog=Database;Integrated Security=True");
dbConnection.Open();
maskedTextBox1.Clear();
dateTimePicker1.Value = DateTime.Now.AddDays(0);
comboBox1.SelectedIndex = -1;
String username = comboBox2.Text;
using (SqlCommand command = new SqlCommand("INSERT INTO [Table Name] (Column Name) VALUES ([Parm1])", dbConnection))
{
command.Parameters.AddWithValue("Parm1", username);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//Close the Window
this.Close();
}
private void label4_Click(object sender, EventArgs e)
{
}
private void comboBox1_DataSourceChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
myConnectionString.Open();
MessageBox.Show("Sql is connected");
myConnectionString.Close();
}
}
}
The VALUES clause should specify a parameter and that parameter should be specified when adding the parameter value. Try substituting the following:
String sqlquery = "INSERT INTO [Man Power] ([Person_Performing_Phase_2]) VALUES ([Parm1])";
SqlCommand command = new SqlCommand(sqlquery, con);
command.Parameters.AddWithValue("Parm1", username);
EDIT
Your connection string is incorrectly formed. I believe the following format is appropriate for your current needs:
String myConnectionString = Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
I would use the following code for inserting data, which opens and closes the connection for each operation. Note the "command.ExecuteNonQuery" statement - its omission is why your insert failed to work - although I am unsure why opening the connection did not throw an error.
private void button3_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection dbConnection = new SqlConnection()) // the "using" construct will close and dispose of the connection
{
dbConnection.ConnectionString = myConnectionString;
dbConnection.Open();
maskedTextBox1.Clear();
dateTimePicker1.Value = DateTime.Now.AddDays(0);
comboBox1.SelectedIndex = -1;
String username = comboBox2.Text;
using (SqlCommand command = new SqlCommand("INSERT INTO [Man Power] ([Person_Performing_Phase_2]) VALUES ([Parm1])", dbConnection))
{
command.Parameters.AddWithValue("Parm1", username);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
You should also remove all other _Click methods and replace the connection and command statement with the myConnectionString assignement.

Categories