Stuart
Stuart

Reputation: 699

How to Execute Update SQL Script in ASP.Net

I have this code to execute a stored procedure (Update SP) in ASP.Net, unfortunately record is not updating when I run the code.

This is my code:

using (SqlConnection sqlConnection = Connt.GetConnection(TblName))
{
        sqlConnection.Open();
        using (SqlDataAdapter adapter = new SqlDataAdapter(SqlScript, sqlConnection))
        {
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.Parameters.AddRange(SqlParam);
        }
}

Where SqlScript is the variable for the stored procedure name and SqlParam is the parameters.

Please help me figure out what is wrong with my code.

Upvotes: 0

Views: 1338

Answers (2)

David J Barnes
David J Barnes

Reputation: 506

Try:

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("ProcedureName", conn) { 
                           CommandType = CommandType.StoredProcedure }) {
   conn.Open();
   command.ExecuteNonQuery();
   conn.Close();
}

With a Param:

command.Parameters.Add(new SqlParameter("@ID", 123));

Upvotes: 1

Angelo
Angelo

Reputation: 335

Hi you can try something like this

SqlConnection sqlConnection = new SqlConnection();
SqlCommand sqlCommand = new SqlCommand();
sqlConnection.ConnectionString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=True";

public void samplefunct(params object[] adparam)
   {
       sqlConnection.Open();
       sqlCommand.Connection = sqlConnection;
       sqlCommand.CommandType = CommandType.StoredProcedure;
       sqlCommand.CommandText = "SPName";

       sqlCommand.Parameters.Add("@param1", SqlDbType.VarChar).Value = adparam[0];
       sqlCommand.Parameters.Add("@param2", SqlDbType.VarChar).Value = adparam[1];
       sqlCommand.Parameters.Add("@Param3", SqlDbType.VarChar).Value = adparam[2];
       sqlCommand.ExecuteNonQuery();
}

Upvotes: 4

Related Questions