Update Query doesnt work asp.net - c#

I'm using the following code to update the data into DB.
There is no error message on console while debugging or on client screen, since parameters are correct. What could be wrong on this?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString);
conn.Open();
SqlCommand actualizar = new SqlCommand();
actualizar.CommandType = CommandType.Text;
actualizar.CommandText = "update Proveedores set proveedores.nombre=#nombre,proveedores.razon_social=#razon,proveedores.rubro =#rubro,proveedores.banco=#banco,proveedores.cuenta_corriente=#cuenta_corriente where proveedores.rut_empresa=#rut";
actualizar.Parameters.AddWithValue("#rut",rut.ToString());
actualizar.Parameters.AddWithValue("#nombre",Nombre.Text);
actualizar.Parameters.AddWithValue("#razon", Razon.Text);
actualizar.Parameters.AddWithValue("#rubro",Rubro.Text);
actualizar.Parameters.AddWithValue("#banco",Banco.Text);
actualizar.Parameters.AddWithValue("#cuenta_corriente",Cuenta.Text);
actualizar.Connection = conn;
actualizar.ExecuteNonQuery();
conn.Close();
Added image on test

Related

Select Query works but Update Query does not work in Postgresql from dot net code using NpgsqlCommand

I am trying to update username from MVC .Net C# after connecting Postgres SQL.
I am able to establish the connection and if I run select command I am able to get the result.
But when I am trying to update record no error comes but updated count comes 0. Record available in database.
Can you please suggest what could be the reason.
using (NpgsqlConnection con = new NpgsqlConnection(connectionString))
{
string query = string.Format("UPDATE um_user SET um_user_name='{0}' WHERE um_user_name='{1}'", updatedUser, userNameToBeUpdated);
con.Open();
NpgsqlCommand comn = new NpgsqlCommand(query, con);
comn.Connection = con;
updatedRows = comn.ExecuteNonQuery();
comn.Dispose();
con.Close();
}
I have added using parameter as well with the following code but still getting 0 updtaed rows.
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "update um_user set um_user_name=#newName where um_user_name=#oldName";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new NpgsqlParameter("#newName", updatedUser));
cmd.Parameters.Add(new NpgsqlParameter("#oldName", userNameToBeUpdated));
updatedRows = cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
}

Adapting C# code for SQL linked server to work directly with Sybase

I have an application written in C# that utilizes a linked server in MS SQL Server. The linked server hosts a Sybase database. I have code that successfully performs a lookup in a table on the linked server, but I have recently been instructed to access the linked server directly, due to performance issues.
I would like to share the code that worked correctly with use of a linked server in the hope that my intention can be easily understood. Then I would like some help on where I am failing to correctly write this to work with Sybase. I readily admit part of the problem is that I'm new to this. This is my first C# application.
private SqlConnection con = new SqlConnection();
int ScanCheck(string Agin, string UCC)
{
//This will open the SQL database connection for checking the
//variables 'Agin' and 'UCC' as lookup values for a single record
//containing both (defined and populated in an earlier part of the code).
//Stored procedure "ScanCheck" is executed. If a record is found, variable #Scans will be
//assigned a value of 1.
//establish connection to db
con.ConnectionString = Properties.Settings.Default.setDBPath;
con.Open();
//set up the sql command to increment hit count.
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "ScanCheck";
cmd.Parameters.AddWithValue("#Agin", Agin);
cmd.Parameters.AddWithValue("#UCC", UCC);
cmd.Parameters.AddWithValue("#Scans", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int Scans = Convert.ToInt32(cmd.Parameters["#Scans"].Value);
cmd.Dispose();
return Scans;
}
//The code above works as intended.
//Here is my attempt to access Sybase directly. I get an error at the line cmd.ExecuteNonQuery();
private AseConnection con = new AseConnection();
int ScanCheck(string Agin, string UCC)
{
con.ConnectionString = Properties.Settings.Default.TMS;
con.Open();
AseCommand cmd = new AseCommand();
cmd.Connection = con;
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select #Scans = count(1) from Database.dbo.Table where whsl_agin_nbr = #Agin and mstr_ucc_cd = #UCC";
cmd.Parameters.AddWithValue("#Agin", Agin);
cmd.Parameters.AddWithValue("#UCC", UCC);
cmd.Parameters.AddWithValue("#Scans", AseDbType.Integer).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
int Scans = Convert.ToInt32(cmd.Parameters["#Scans"].Value);
cmd.Dispose();
return Scans;
}
The error I receive is:
Sybase.Data.AseClient.AseException {"Unsupported parameter type"}

Data wont delete from table

I cannot get my program to clear an SQL database on opening. I have went through debugging and there are no errors in the code for deleting, but it just doesn't do it
string delete = "Delete from Trivia";
con.Open();
SqlCommand comm = new SqlCommand(delete, con);
con.Close();
You're not actually executing the command. You might also consider a using statement. That will automatically dispose of the connection properly, even if an error occurs:
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand comm = new SqlCommand("delete from Trivia", con);
comm.ExecuteNonQuery();
}

Getting ORA-01036 error when using output parameter in C#

I am having a problem with an Output parameter in C#/Oracle. I have isolated the code that I need to get working.
This is part of a much larger SQL statement, so do not worry too much if it doesn't make sense. In short I need to copy a row, give it a new ID and return that new ID. I tried using "RETURNING" which did not work. I see no reason why the code below should not work, but I'm getting an "ORA-01036: illegal variable name/number" error. Can anyone see what I'm doing wrong?
using (OracleConnection conn = new OracleConnection(connString))
{
// Open connection and create command.
conn.Open();
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("outValue", OracleType.Int32).Direction = ParameterDirection.Output;
cmd.CommandText = "SELECT seq.nextval INTO :outValue FROM dual";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
// This is just to see the exception when it fails.
}
}
}
The name of the parameter doesn't match.
cmd.Parameters.Add(":outValue", OracleType.Int32).Direction.......;
^
I have also seen this variation on the query syntax
"BEGIN SELECT seq.nextval INTO :outValue FROM dual END;"
You are using named parameters. Try setting:
cmd.BindByName = true;
Have you tried 'returning' keyword like this?
This code works for me.
using (OracleConnection conn = new OracleConnection(connString))
{
// Open connection and create command.
conn.Open();
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("outValue", OracleType.Int32).Direction = ParameterDirection.Output;
cmd.CommandText = "insert into table (id, value) values (seq.nextval, 'value') returning id into :outValue";
cmd.ExecuteNonQuery();
}
}

inserting and deleting not happening in local database (.sdf)

When I try to select values from a local database it executes without any issue. But when I try to insert and delete it's executing the query but it's not affecting any rows.
This is the code I'm using to delete row from my local database:
SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = Database1.sdf";
SqlCeCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "DELETE FROM table1 WHERE slno=2";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.Prepare();
int aff=cmd.ExecuteNonQuery();//here its returning '0'
MessageBox.Show(aff.ToString());
sqlConnection1.Dispose();
sqlConnection1.Close();
It is possible that the delete won't affect any rows.
As an aside, I would advise using the using statement in your code:
using (SqlCeConnection conn = new SqlCeConnection("Data Source = Database1.sdf"))
using (SqlCeCommand comm = new SqlCeCommand("DELETE FROM table1 WHERE slno = 2", conn))
{
conn.Open();
comm.CommandType = CommandType.Text;
comm.ExecuteNonQuery();
}
This will handle disposal of relevant objects for you.
Assuming that the SQL is relevant to your database and that you have successfully connected using the connection string beforehand, then the above could will perform a delete action against your database.

Categories