user2936417
user2936417

Reputation: 11

update does not want to work on my windows form

I have an update stored procedure which works perfectly on my sql , yet when I try to update on my c# form it doesn't update , yet my code seems to be right , im not sure why cant I perform an update this is my update stored procedure sql script USE [MediaPlayer] GO /* Object: StoredProcedure [dbo].[updateBooks] Script Date: 11/25/2013 07:51:33 */ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

ALTER PROCEDURE [dbo].[updateBooks]
    -- Add the parameters for the stored procedure here
    @Name nvarchar (50),
    @FileName nvarchar(50),
    @FilePath  nvarchar(50),
    @FileSize float,
    @DateAdded date,
    @MediaLength nvarchar (50),
    @MediaSubType nvarchar (50),
    @MediaType nvarchar (50),
    @Thumbnail image,
    @DateAquired datetime,
    @BooksName nvarchar (50),
    @Publisher nvarchar(50),
    @Author nvarchar(50),
    @YearOfPublication date, 
    @Genre nvarchar (50),
    @ISBN nvarchar (50),
    @Synoposis nvarchar(max),
    @SeriesTitle nvarchar(50),
    @SeriesNumber nvarchar (50),
    @BookCover image,
    @GeneralID int output,
    @BookID int output

AS
BEGIN
 update dbo.Book
 SET 

 BooksName=@BooksName,
 Publisher=@Publisher, 
 Author =@Author,
 [Year of publication] =@YearOfPublication,
 Genre =@Genre,
 ISBN=@ISBN, 
 Synoposis=@Synoposis,
 [Series Title]= @SeriesTitle,
 [Series Number] =@SeriesNumber,
 [Book Cover] =@BookCover

 from Book
 Where  BookID = @BookID 
 select @@ROWCOUNT 
update dbo.General
SET

Name =@Name, 
FileName= @FileName, 
FilePath= @FilePath,
FileSize=@FileSize, 
DateAdded= @DateAdded, 
MediaLength =@MediaLength,
MediaSubType = @MediaSubType, 
MediaType = @MediaType, 
Thumbnail =@Thumbnail, 
DateAquired= @DateAquired

 where GeneralID = @GeneralID 
 END

C# code

private void DoUpdate()
        {
            try
            {
                string picLoc = "C:\\Users\\Dee\\Pictures\\PIC\\download.jpg";
                try
                {
                    byte[] img = null;
                    FileStream fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    img = br.ReadBytes((int)fs.Length);

                    string connectionString = "Data Source=(local);Initial Catalog=MediaPlayer;Integrated Security=True";
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();

                        SqlCommand cmd = new SqlCommand("dbo.updateBooks", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Name", SqlDbType.NVarChar).Value = NametextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@FileName", SqlDbType.NVarChar).Value = FileNametextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@FileSize", SqlDbType.Float).Value = Convert.ToDouble(fileSizetextBox.Text);
                        cmd.Parameters.AddWithValue("@FilePath", SqlDbType.NVarChar).Value = FilePathtextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@DateAdded", SqlDbType.Date).Value = DateAddeddateTimePicker.Text.ToString();
                        cmd.Parameters.AddWithValue("@MediaLength", SqlDbType.NVarChar).Value = MediaLengthtetextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@MediaSubType", SqlDbType.NVarChar).Value = MediaSubtypetextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@MediaType", SqlDbType.NVarChar).Value = MediaTypetextBox.Text.ToString();
                        cmd.Parameters.Add(new SqlParameter("@Thumbnail", img));
                        cmd.Parameters.AddWithValue("@DateAquired", SqlDbType.DateTime).Value = DateAquiredDatetimepicker.Text.ToString();
                        cmd.Parameters.AddWithValue("@Author", SqlDbType.NVarChar).Value = AuthortextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@Publisher", SqlDbType.NVarChar).Value = PublishertextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@BooksName", SqlDbType.NVarChar).Value = BooksNametextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@SeriesTitle", SqlDbType.Date).Value = SeriesTitletextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@SeriesNumber", SqlDbType.NVarChar).Value = SeriesNumberTextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@Genre", SqlDbType.NVarChar).Value = genretextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@Synoposis", SqlDbType.NVarChar).Value = SynoposistextBox.Text.ToString();
                        cmd.Parameters.AddWithValue("@YearOfPublication", SqlDbType.NVarChar).Value = YearOfPublicationdatetimepicker.Text.ToString();
                        cmd.Parameters.AddWithValue("@ISBN ", SqlDbType.NVarChar).Value = ISBNtextBox.Text.ToString();
                        cmd.Parameters.Add(new SqlParameter("@BookCover", img));
                        SqlParameter parm1 = new SqlParameter("@GeneralID", SqlDbType.Int);
                        parm1.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(parm1);
                        SqlParameter parm = new SqlParameter("@BookID", SqlDbType.Int);
                        parm.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(parm);
                        //cmd.ExecuteNonQuery();

                        MessageBox.Show(cmd.ExecuteNonQuery().ToString() + " record(s) Updated.");
                       // MessageBox.Show("record successfully updated!");
                        //clrtxtb();
                        conn.Close();

                    }
                }



                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show("error is " + ex.Message);
                }
            }
            finally
            { }

        }
enter code here

Upvotes: 1

Views: 300

Answers (3)

Larry
Larry

Reputation: 18031

The two WHERE clauses in stored procedures use the parameters @BookID and @GeneralID.

Because of this, both are supposed to contain a value when the stored procedure is called.

Instead, no value is passed to those parameters. Furthermore, both are declared as OUTPUT.

That's the reason why nothing is updated.

Upvotes: 0

Angelo
Angelo

Reputation: 335

hi have you tried something like this

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

   sqlConnection.Open();
   sqlCommand.Connection = sqlConnection;
   sqlCommand.CommandType = CommandType.StoredProcedure;
   sqlCommand.CommandText = "SPName";

   sqlCommand.Parameters.Add("@param1", SqlDbType.VarChar).Value = value1;
   sqlCommand.Parameters.Add("@param2", SqlDbType.VarChar).Value = value2;
   sqlCommand.Parameters.Add("@Param3", SqlDbType.VarChar).Value = value3;
   sqlCommand.ExecuteNonQuery();

Upvotes: 0

fen89
fen89

Reputation: 579

What about first uncommenting the line cmd.ExecuteNonQuery(); ?

Upvotes: 0

Related Questions