My requirement is if the user click on update data with out changing any field on the form i would like to show as No changes made and if any changes i would like to update the data
I have written a Routine for updating data as follows
CREATE DEFINER=`root`#`%` PROCEDURE `uspEmployeeFaxDetailsUpdate`(_EmpID int(11),
_FaxNumberTypeID varchar(45),
_FaxNumber decimal(10,0),
_EndDate datetime)
BEGIN
declare p_ecount int;
set p_ecount= (select count(1) from tblemployeefaxdetails where
FaxNumberTypeID=_FaxNumberTypeID and
FaxNumber=_FaxNumber and
EndDate='9999-12-31');
if p_ecount=0 then
begin
update tblemployeefaxdetails
set
EndDate=_EndDate WHERE EmpID=_EmpID and EndDate="9999-12-31";
insert into tblemployeefaxdetails(EmpID,FaxNumberTypeID,FaxNumber,StartDate,EndDate) values
(_EmpID,_FaxNumberTypeID,_FaxNumber,curdate(),'9999-12-31');
end;
end if;
END
I am getting some time my required message but some time it is showing the update message
This is my code on update
oEmployeePersonalData.EmpID = EmpID;
oEmployeePersonalData.FaxNumberTypeID = ddlFaxTypeID.SelectedItem.Text;
oEmployeePersonalData.FaxNumber = Convert.ToInt64(txtFaxNumber.Text);
oEmployeePersonalData.EndDate = DateTime.Today.AddDays(-1);
if (oEmployeePersonalData.FaxDetailUpdate())
{
oMsg.Message = "Updated Sucessfully";
Label m_locallblMessage;
oMsg.AlertMessageBox(out m_locallblMessage);
Page.Controls.Add(m_locallblMessage);
}
else
{
oMsg.Message = "Not Sucessfully";
Label m_locallblMessage;
oMsg.AlertMessageBox(out m_locallblMessage);
Page.Controls.Add(m_locallblMessage);
}
Updated code
public bool FaxDetailUpdate()
{
m_bFlag = false;
try
{
m_oCmd = new MySqlCommand(StoredProcNames.tblEmployeeFaxdetails_uspEmployeeFaxdetailsUpdate, m_oConn);
m_oCmd.CommandType = CommandType.StoredProcedure;
m_oCmd.Parameters.AddWithValue("_EmpID", EmpID);
m_oCmd.Parameters.AddWithValue("_FaxNumberTypeID", FaxNumberTypeID);
m_oCmd.Parameters.AddWithValue("_FaxNumber", FaxNumber);
m_oCmd.Parameters.AddWithValue("_EndDate", EndDate);
if (m_oConn.State == ConnectionState.Closed)
{
m_oConn.Open();
}
if ((m_oCmd.ExecuteNonQuery()) > 0)
{
this.m_bFlag = true;
}
}
catch (MySqlException oSqlEx)
{
m_sbErrMsg.Length = 0;
m_sbErrMsg = Utilities.SqlErrorMessage(oSqlEx);
//DB write the Error Log
m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
}
catch (Exception oEx)
{
m_sbErrMsg = Utilities.ErrorMessage(oEx);
//DB write the Error Log
m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
}
finally
{
m_oConn.Close();
}
return this.m_bFlag;
}
I am not getting any error but i would like to be done as per i said
Can any one tell what changes i have to made in this
I do not really understand the routine, what the magic date 9999-12-31 represents, and why a new record is inserted every time, instead of updating the old one.
What you can do, is have the routine return a indicator if the row was changed or not, returning the value of p_ecount through a OUT parameter.
For more information on how to use OUT parameters using the .net MySql client see http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-stored.html
Related
I'm getting the error code SQLITE_BUSY when trying to write to a table after selecting from it. The select statement and result is properly closed prior to my insert.
If I'm removing the select part the insert works fine. And this is what I'm not getting. According to the documentation SQLITE_BUSY should mean that a different process or connection (which is definetly not the case here) is blocking the database.
There's no SQLite manager running. Also jdbcConn is the only connection to the database I have. No parallel running threads aswell.
Here's my code:
try {
if(!jdbcConn.isClosed()) {
ArrayList<String> variablesToAdd = new ArrayList<String>();
String sql = "SELECT * FROM VARIABLES WHERE Name = ?";
try (PreparedStatement stmt = jdbcConn.prepareStatement(sql)) {
for(InVariable variable : this.variables.values()) {
stmt.setString(1, variable.getName());
try(ResultSet rs = stmt.executeQuery()) {
if(!rs.next()) {
variablesToAdd.add(variable.getName());
}
}
}
}
if(variablesToAdd.size() > 0) {
String sqlInsert = "INSERT INTO VARIABLES(Name, Var_Value) VALUES(?, '')";
try(PreparedStatement stmtInsert = jdbcConn.prepareStatement(sqlInsert)) {
for(String name : variablesToAdd) {
stmtInsert.setString(1, name);
int affectedRows = stmtInsert.executeUpdate();
if(affectedRows == 0) {
LogManager.getLogger().error("Error while trying to add missing database variable '" + name + "'.");
}
}
}
jdbcConn.commit();
}
}
}
catch(Exception e) {
LogManager.getLogger().error("Error creating potentially missing database variables.", e);
}
This crashes on int affectedRows = stmtInsert.executeUpdate();. Now if I remove the first block (and manually add a value to the variablesToAdd list) the value inserts fine into the database.
Am I missing something? Am I not closing the ResultSet and PreparedStatement properly? Maybe I'm blind to my mistake from looking at it for too long.
Edit: Also executing the select in a separate thread does the trick. But that can't be the solution. Am I trying to insert into the database too fast after closing previous statements?
Edit2: I came across a busy_timeout, which promised to make updates/queries wait for a specified amount of time before returning with SQLITE_BUSY. I tried setting the busy timeout like so:
if(jdbcConn.prepareStatement("PRAGMA busy_timeout = 30000").execute()) {
jdbcConn.commit();
}
The executeUpdate() function still immedeiately returns with SQLITE_BUSY.
I'm dumb.
I was so thrown off by the fact that removing the select statement worked (still not sure why that worked, probably bad timing) that I missed a different thread using the same file.
Made both threads use the same java.sql.Connection and everything works fine now.
Thank you for pushing me in the right direction #GordThompson. Wasn't aware of the jdbc:sqlite::memory: option which led to me finding the issue.
We are using asp.net mvc as web application and oracle 12C as database, 'Oracle.DataAccess.Client'(12.1.0, 64 bit version) for connecting to Oracle Database, and our asp.net MVC application runs on 64 bit machine as well.
One of our stored procedures returns a cursor as out put parameter.
Now, the same stored procedure takes little less than a second when we execute it on TOAD or SQL Developer, but when C# executes the same stored procedure it takes about 35 seconds..!!! to return back. TOAD, SQL Developer and asp.net iis server all are on my local machine and the Oracle server is on different machine.
In C# i have logged all kinds of timings, like how long it took to open a connection, on what time stamp C# is calling stored procedure and on what time it completed and came back, how long it took to convert DataSet to C# List. after observing all timing we see that following statement takes 35 seconds
OracleDataAdapter loDataAdapter = new OracleDataAdapter(loCommand);
LogInfo("fill DataTable");//logs time stamp saying start
loDataAdapter.Fill(loDataTable);
LogInfo("fill DataTable",false);//logs time stamp saying Done
loDataAdapter = null;
We thought it could be network issue, if that is the case the toad and sql developer should also take same time, so network is not an issue.
What we Tried:
The only suspect i have is ODP.NET Driver, so removed all oracle client(s) and reinstalled them, No use.
Now trying to see if there is some thing i have to configure on sqlnet.ora or tns file connection ? but could not find any...
Here is the complete code
public DataTable GetDataTable(string psStoredProcedure)
{
LogInfo(psStoredProcedure);
//******************************************************************
//Function.........GetDataTable
//Description......Gets a data set (with the schema) from stored procedure
//Input Param......(1)Stored procedure name
// (2)Boolean to determine if we want to get the schema
//Output...........Return DataTable
//
//******************************************************************
OracleCommand loCommand = new OracleCommand();
OracleConnection loConnection = new OracleConnection(msConnectionString);
DataTable loDataTable = new DataTable();
//---Main Execution Block
try
{
if (!(loConnection.State == ConnectionState.Open))
{
LogInfo("Open Conn");
loConnection.Open();
LogInfo("Open Conn",false);
}
//---Define the SelectCommand
if (moCommand == null)
{
loCommand = new OracleCommand();
}
else
{
loCommand = moCommand;
moCommand = null;
}
loCommand.CommandText = psStoredProcedure;
loCommand.Connection = loConnection;
loCommand.CommandType = CommandType.StoredProcedure;
//---Populate the dataset
OracleDataAdapter loDataAdapter = new OracleDataAdapter(loCommand);
LogInfo("fill DataTable");
loDataAdapter.Fill(loDataTable);
LogInfo("fill DataTable",false);
loDataAdapter = null;
}
catch (Exception oExcep)
{
throw oExcep;
}
finally
{
//---Close the connection
if (loConnection.State == ConnectionState.Open)
{
LogInfo("Close");
loConnection.Close();
LogInfo("Close", false);
}
loCommand = null;
loConnection = null;
}
LogInfo(psStoredProcedure, false);
return loDataTable;}
Following is the PL/SQL In stored procedure:
CREATE OR REPLACE PROCEDURE REF_LIST_INQ (
ivID IN VARCHAR2 DEFAULT NULL,
ivAID IN VARCHAR2 DEFAULT NULL,
ivDID IN VARCHAR2 DEFAULT NULL,
ivSelection IN VARCHAR2 DEFAULT NULL,
ivStartDate IN VARCHAR2 DEFAULT NULL,
ivEndDate IN VARCHAR2 DEFAULT NULL,
ocRefList OUT ES_PACKAGE.cursorType,
ovReturnCode OUT VARCHAR2,
ovErrorMsg OUT VARCHAR2
)
AS
cmdSQL VARCHAR2(4000) := NULL;
whereCause VARCHAR2(2000) := NULL;
selectCause VARCHAR2(2000) := NULL;
BEGIN
/************
--Hundreds of lines of PL/SQL Code for
-- dynamically creating sql statements
-- and assigning it to 'selectCause' and 'whereCause'
*************************/
cmdSQL := selectCause || whereCause || ' ORDER BY SD.MODIFIED_DATE DESC, SD.REFERRAL_NUMBER';
OPEN ocRefList FOR cmdSQL;
ovReturnCode := '0';
EXCEPTION WHEN OTHERS THEN
ovErrorMsg := 'REF_LIST_INQ - ' || SUBSTR(SQLERRM,1,200);
ovReturnCode := '-1';
END REF_LIST_INQ;
/
For me this is an answer, may be if someone is facing same situation can try it out.
After researching few days, following C# statement helped me.
loCommand.FetchSize = loCommand.FetchSize * 18192;
As per my understanding (i am not good at describing, trying my level best) this works only for 64 bit version, and if we do not specify 'FetchSize' it will keep fetching predefined size (i believe 128k) of data from cursor opened at Oracle DB, it will do round trips until the data provided by the cursor is done.
So here is the complete C# code, but that one line code made a lot difference.
//******************************************************************
//Function.........GetDataTable
//Description......Gets a data set (with the schema) from stored procedure
//Input Param......(1)Stored procedure name
// (2)Boolean to determine if we want to get the schema
//Output...........Return DataTable
//
//******************************************************************
OracleCommand loCommand = new OracleCommand();
OracleConnection loConnection = new OracleConnection(msConnectionString);
DataTable loDataTable = new DataTable();
//---Main Execution Block
try
{
if (!(loConnection.State == ConnectionState.Open))
{
LogInfo("Open Conn");
loConnection.Open();
LogInfo("Open Conn",false);
}
//---Define the SelectCommand
if (moCommand == null)
{
loCommand = new OracleCommand();
}
else
{
loCommand = moCommand;
moCommand = null;
}
loCommand.CommandText = psStoredProcedure;
loCommand.Connection = loConnection;
loCommand.CommandType = CommandType.StoredProcedure;
//Here is the Hero
loCommand.FetchSize = loCommand.FetchSize * 18192;
//---Populate the dataset
OracleDataAdapter loDataAdapter = new OracleDataAdapter(loCommand);
LogInfo("fill DataTable");
loDataAdapter.Fill(loDataTable);
LogInfo("fill DataTable",false);
loDataAdapter = null;
}
catch (Exception oExcep)
{
throw oExcep;
}
finally
{
//---Close the connection
if (loConnection.State == ConnectionState.Open)
{
LogInfo("Close");
loConnection.Close();
LogInfo("Close", false);
}
loCommand = null;
loConnection = null;
}
LogInfo(psStoredProcedure, false);
return loDataTable;}
can anybody tell me where I am doing wrong.I am trying to develop a small site on my own using c#.I am unable to show the record which was fetched from datareader.
{
SqlConnection cone = new SqlConnection("User id=...;Password=;...");
SqlCommand conecmd = new SqlCommand("select bankbalance from bank where bankpassword='" + txtboxbankpassword.Text + "'", cone);
cone.Open();
SqlDataReader drcone = conecmd.ExecuteReader();
if (drcone.HasRows) // says it has row(s) i.e., Hasrows=true
{
while (drcone.Read()) // here its value is false
{
lblremainingbankbalance.Text = drcone["bankbalance"].ToString();
}
int a;
a = Convert.ToInt32(lblremainingbankbalance.Text);
lblmsg.Text = "Transaction successful.Please wait while we redirect you to mypage and your current " + bankstore + " accountbalance is " + a + "";
Response.AddHeader("REFRESH", "5;URL=Mypage.aspx");
txtboxbankpassword.Text = "";
} else {
lblmsg.Text = "Norecord(s)";
}
}
Try to run your query in SQL server and see if it shows some rows because i don't see any errors in code also trim the text of your textbox like this
txtboxbankpassword.Text.Trim()
Also close you connection when you are done with reading rows from datareader.
your case is very strange.. if drcone.HasRowsis true drcone.Read() have to return true... minimum for one time..
Are you debugging the code and add drcone.Read() to quickwatch or have it in your watchlist? Because, then it will run the method Read() before your while loop. Next time when you call Read() (in your while-loop) it will return false..
You get one row, the reader aligns the reader to the first row (in whatchlist), and returns false in your while loop and saying "there are no rows left after this one".
See this link Here for the documentation on this: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx
if you have only a singlerow, the reader "is on position -1".. after drcone.Read() (return true) set the position to 0 (1 and last row).. next time you call drcone.Read() it will return false.. because there are no rows left after this one (reader is on position 0)
I want to output an additional variable that's not in my table, can someone explain how to do this, or why it's not possible?
ie -
OPEN V_CURSOR FOR SELECT ee.EmployeeID, v_overtime_worked FROM EMPLOYEE_RECORDS ee;
So basically you want to store in that cursor, data from the table, and another variable v_overtime_worked, that has nothing to do with the table?
What error are you receiving?
What I know for sure is that this is working...so basically you can put another variable in the cursor:
declare
c_Curs SYS_REFCURSOR;
v_overtime_worked VARCHAR2(120) := '1000';
v_book_title VARCHAR2(255) := '';
v_xxx VARCHAR2(120);
BEGIN
OPEN c_Curs FOR SELECT book_title, v_overtime_worked FROM books;
LOOP
FETCH c_Curs INTO v_book_title, v_xxx;
EXIT WHEN c_Curs%NOTFOUND OR c_Curs IS NULL;
DBMS_OUTPUT.PUT_LINE(v_book_title || v_xxx);
end loop;
close c_Curs;
END;
As for your Java problem I'm afraid I don't know the answer..and i would need to do research :)
Maybe you need something like this :
#Override
public void execute(Connection con) throws SQLException {
CallableStatement call = con.prepareCall(ProcedureCalls.GET_ALL_BOOKS);
call.registerOutParameter(1, OracleTypes.CURSOR);
call.execute();
ResultSet rs = (ResultSet) call.getObject(1);
if(rs!=null){
while (rs.next ()) {
Book b = new Book();
b.setBookId(rs.getLong("book_id"));
b.setBookTitle(rs.getString("title"));
b.setBookYear(rs.getInt("book_year"));
b.setBookAuthor(rs.getString("author"));
book_list.add(b);
}
if(rs!=null) rs.close();
if(call!= null) call.close();
}
}
I'm trying to do a search on a table in my database where it returns the top 50 rows with a firstname like the search term being passed to the function, but its always returning the same 50 results
My sql looks like this:
Select TOP(50) *
FROM [database].[dbo].[records]
WHERE (A_1STNAME LIKE '" + #searchTerm + "%')
ORDER BY A_RECID
When I run this query in Visual Studios query window, it works as expected but when I run it through my ASP.NET application, it always returns the same 50 results, and only one of them has a first name close to the searchTerm I passed to it.
here is the page code that runs the function:
protected void Page_Load(object sender, EventArgs e)
{
_migrated_data data = new _migrated_data();
DataSet ds = data.Search(Request.QueryString.Get("query"), "A_RECID", 50);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
rpt_Data.DataSource = ds.Tables[0].DefaultView;
rpt_Data.DataBind();
}
}
and here is the search method of _migrated_data:
public DataSet Search(String #pSearchTerm, String #pSortBy, int #pRowCount)
{
DataSet ds = new DataSet();
OleDbConnection objOleDBConn;
OleDbDataAdapter objOleDBDa;
objOleDBConn = new OleDbConnection(ClearingHouse_OLEDDB);
objOleDBConn.Open();
string lSQL = "SELECT TOP(50) * FROM [database].[dbo].[records]";
lSQL += " WHERE (A_1STNAME LIKE #searchTerm ) ORDER BY #sortBy";
SqlCommand t = new SqlCommand(lSQL);
if (pSearchTerm != null && pSearchTerm != "")
{
t.Parameters.AddWithValue("#searchTerm", #pSearchTerm + "%");
}
if (pSortBy != null && pSortBy != "")
{
t.Parameters.AddWithValue("#sortBy", #pSortBy);
}
else
{
t.Parameters.AddWithValue("#sortBy", "A_RECID");
}
objOleDBDa = new OleDbDataAdapter(t.CommandText, objOleDBConn);
objOleDBDa.SelectCommand.CommandType = CommandType.Text;
objOleDBDa.Fill(ds);
objOleDBConn.Close();
return ds;
}
Using locals to view the final CommandText of t, I get the sql results I gave above.
Any help is greatly appriciated :)
AS Rob Rodi said, first of all, to get the results begining with a value you will need % char at the end of the therm:
Select TOP(50) *
FROM [database].[dbo].[records]
WHERE (A_1STNAME LIKE '" + #searchTerm + "%')
ORDER BY A_RECID
Second, probably your searchTerm is empty, so it's grabbing always the same results.
Debug your app and watch the variable to see if it's receiving some value.
You are having SQL injection there. Also it looks like the %-signs are missing.
The fact, that the query works in a query window and not in your app means, that the error is not in your query! Did you think about that? Probably, you should post code of your ASP.NET app.
It sounds like your parameter isn't being correctly passed to your data layer. Easiest way to find out if that's the case is to turn on Sql Profiler and check to see if it's being passed. If it is, your sql's to blame. If it's not, it's your application. You can then use the debugger on your application to work your way up the stack to see where the problem lies.