P
Peter
We have a simple LoginUser stored proc that returns the
user name and other info to the calling code.
BEGIN Tran
SELECT ...
FROM...
WHERE...
IF @@error <> 0
...
IF @@rowcount <> 1
begin
RAISERROR..
RETURN -1
end
COMMIT TRAN
RETURN 1
The interesting thing is that even though the Fill method
on the .net side retrieves a record and continues without
exception (see .net code below), the @@rowcount check
returns 0 and gives RAISERROR (but the client doesn't
catch it). The SP runs perfectly in Interactive SQL and
rowcount returns 1.
The theory is that the Fill method opens and closes a
connection and as such resets the @@rowcount in the db
prior to the SP checking it. Is that possible ? What are
we doing wrong ??? Note, all our data retrieval,
transaction handling, and business rule checking is done
in stored procs, should we use the DataReader instead of
the DataAdapter... how would any links or sample code
pointers are appreciated.
Thanks, Peter
public LoginUserWrapper()
{
this.connection = (OdbcConnection)
DbConnection.CreateDbConnection();
dataSet = new DataSet();
adapter = new OdbcDataAdapter();
adapter.SelectCommand =
createSelectCommand();
dataTable = null;
currentRow = -1;
}
// Create Select Command
private OdbcCommand createSelectCommand()
{
String cmdText = "CALL LoginUser
(?, ?)";
OdbcCommand cmd = new OdbcCommand
(cmdText, connection);
OdbcParameter p;
p = cmd.Parameters.Add(new
OdbcParameter("@Email", OdbcType.Char, 60));
p.Direction =
ParameterDirection.Input;
p = cmd.Parameters.Add(new
OdbcParameter("@pwd", OdbcType.Char, 20));
p.Direction =
ParameterDirection.Input;
return cmd;
}
// Create Insert Command
// Create Insert1 Command
// Create Update Command
// Create Delete Command
// Create Delete1 Command
// Create Empty Table
public void createEmptyTable()
{
dataTable = new DataTable("data");
DataColumn column;
// Create and add columns
column = new DataColumn();
column.DataType = System.Type.GetType
("System.String");
column.AllowDBNull = true;
column.Caption = "USER_NAME";
column.ColumnName = "USER_NAME";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.Int32");
column.AllowDBNull = true;
column.Caption = "USER_ID";
column.ColumnName = "USER_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.Int32");
column.AllowDBNull = true;
column.Caption = "MODEL_ID";
column.ColumnName = "MODEL_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.String");
column.AllowDBNull = true;
column.Caption = "LANG_ID";
column.ColumnName = "LANG_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
}
// Select Command
public void Select(string email, string
pwd)
{
try
{
adapter.SelectCommand.Parameters["@Email"].Value = email;
adapter.SelectCommand.Parameters["@pwd"].Value = pwd;
connection.Open();
try
{
adapter.SelectCommand.Prepare();
adapter.Fill
(dataSet, "data");
dataTable =
dataSet.Tables[0];
if
(dataTable.Rows.Count > 0)
{
currentRow
= 0;
}
}
finally
{
connection.Close();
}
}
catch (OdbcException e1)
{
if (errors != null)
{
for (int i = 0; i
< e1.Errors.Count; i++)
{
Utils.AddError(ref errors, Utils.GetErrorMessage(e1.Errors
));
}
}
}
catch (Exception e1)
{
if (errors != null)
{
Utils.AddError(ref
errors, "General Error: " + e1.Message);
}
}
}
user name and other info to the calling code.
BEGIN Tran
SELECT ...
FROM...
WHERE...
IF @@error <> 0
...
IF @@rowcount <> 1
begin
RAISERROR..
RETURN -1
end
COMMIT TRAN
RETURN 1
The interesting thing is that even though the Fill method
on the .net side retrieves a record and continues without
exception (see .net code below), the @@rowcount check
returns 0 and gives RAISERROR (but the client doesn't
catch it). The SP runs perfectly in Interactive SQL and
rowcount returns 1.
The theory is that the Fill method opens and closes a
connection and as such resets the @@rowcount in the db
prior to the SP checking it. Is that possible ? What are
we doing wrong ??? Note, all our data retrieval,
transaction handling, and business rule checking is done
in stored procs, should we use the DataReader instead of
the DataAdapter... how would any links or sample code
pointers are appreciated.
Thanks, Peter
public LoginUserWrapper()
{
this.connection = (OdbcConnection)
DbConnection.CreateDbConnection();
dataSet = new DataSet();
adapter = new OdbcDataAdapter();
adapter.SelectCommand =
createSelectCommand();
dataTable = null;
currentRow = -1;
}
// Create Select Command
private OdbcCommand createSelectCommand()
{
String cmdText = "CALL LoginUser
(?, ?)";
OdbcCommand cmd = new OdbcCommand
(cmdText, connection);
OdbcParameter p;
p = cmd.Parameters.Add(new
OdbcParameter("@Email", OdbcType.Char, 60));
p.Direction =
ParameterDirection.Input;
p = cmd.Parameters.Add(new
OdbcParameter("@pwd", OdbcType.Char, 20));
p.Direction =
ParameterDirection.Input;
return cmd;
}
// Create Insert Command
// Create Insert1 Command
// Create Update Command
// Create Delete Command
// Create Delete1 Command
// Create Empty Table
public void createEmptyTable()
{
dataTable = new DataTable("data");
DataColumn column;
// Create and add columns
column = new DataColumn();
column.DataType = System.Type.GetType
("System.String");
column.AllowDBNull = true;
column.Caption = "USER_NAME";
column.ColumnName = "USER_NAME";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.Int32");
column.AllowDBNull = true;
column.Caption = "USER_ID";
column.ColumnName = "USER_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.Int32");
column.AllowDBNull = true;
column.Caption = "MODEL_ID";
column.ColumnName = "MODEL_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType
("System.String");
column.AllowDBNull = true;
column.Caption = "LANG_ID";
column.ColumnName = "LANG_ID";
// column.DefaultValue = 0;
dataTable.Columns.Add(column);
}
// Select Command
public void Select(string email, string
pwd)
{
try
{
adapter.SelectCommand.Parameters["@Email"].Value = email;
adapter.SelectCommand.Parameters["@pwd"].Value = pwd;
connection.Open();
try
{
adapter.SelectCommand.Prepare();
adapter.Fill
(dataSet, "data");
dataTable =
dataSet.Tables[0];
if
(dataTable.Rows.Count > 0)
{
currentRow
= 0;
}
}
finally
{
connection.Close();
}
}
catch (OdbcException e1)
{
if (errors != null)
{
for (int i = 0; i
< e1.Errors.Count; i++)
{
Utils.AddError(ref errors, Utils.GetErrorMessage(e1.Errors
));
}
}
}
catch (Exception e1)
{
if (errors != null)
{
Utils.AddError(ref
errors, "General Error: " + e1.Message);
}
}
}