0

I have update query which was working but I dont know what I have changed now it doesn't work just messagebox message is pop up but code update doesnot work on database.

 private void btnUpdate_Click(object sender, EventArgs e)
    {
        var database = DBConnection.DBConnect();
        SqlCommand cmd = new SqlCommand("dbo.Customer_update", database);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Id",SqlDbType.Int).Value = txtId.Text;
        cmd.Parameters.Add("@Firstname", SqlDbType.NVarChar).Value = txtFirstName.Text;
        cmd.Parameters.Add("@Lastname", SqlDbType.NVarChar).Value = txtLastName.Text;
        cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtCity.Text;
        cmd.Parameters.Add("@Country", SqlDbType.NVarChar).Value = txtCountry.Text;
        cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = txtPhone.Text;
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        adapter.Fill(ds);
        ShowCustomers();
        MessageBox.Show("Succeccfully updated into DataBase");           
    }       

and here is stored procedure

ALTER procedure [dbo].[Customer_update] 
( @Id int null ,
@Firstname nvarchar(40)=null,
@Lastname nvarchar(40)=null,
@City nvarchar(40)=null,
@Country nvarchar(40)=null,
@Phone nvarchar(20)=null
)
as 
begin 
update Customer 
set FirstName=@FirstName,
LastName=@Lastname,
City=@City,
Country=@Country,
Phone=@Phone
where Id=@Id 
end 
Shadow
  • 121
  • 1
  • 1
  • 8
  • 3
    The `visual-studio` tag is only intended for questions relating to the Visual Studio application, and not code written within. – ProgrammingLlama Apr 12 '19 at 14:01
  • Why did you remove the [tag:ado.net] tag? You are using ADO! –  Apr 12 '19 at 14:03
  • Your USP returns no data btw, use ExecuteNonQuery() instead of an Adapter & DataTable. – Alex K. Apr 12 '19 at 14:05
  • You never call `adapter.Update(ds)`. – RWRkeSBZ Apr 12 '19 at 14:06
  • Is the button being called? If it is, I'd recommend changing the title because the button is then working. – RedX Apr 12 '19 at 14:08
  • @Alex K can you tell me how to do that I am learning so need your help sir – Shadow Apr 12 '19 at 14:09
  • DataAdapter.Fill() is used to select data whereas DataAdapter.Update() is used to update data. You can find a nice comparison here : https://stackoverflow.com/questions/14305447/comparison-of-dataadapter-fill-and-update You should use ExecuteNonQuery() instead. Here's the example- https://www.c-sharpcorner.com/blogs/insert-delete-and-update-using-stored-procedure-in-asp-net1 – coffeebeans Apr 12 '19 at 14:26

0 Answers0