Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I need help with an odd problem I'm experiencing with my update. I have an update stored procedure in SQL and I want to use that update in c# form , iv don that yet my problem is that my update does not work on my form, it says that records have been updated yet when I check on SQL nothing has been updated. my update stored procedure works perfectly when I execute it, so I'm not sure where the problem lies. below I will post both my update c# code and stored procedure script.
thanks in advance


C#
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);
                         MessageBox.Show(cmd.ExecuteNonQuery().ToString() + " record(s) Updated.");
                        conn.Close();

                    }
                }



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

        }
SQL
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
Posted
Updated 24-Nov-13 22:22pm
v2
Comments
coded007 25-Nov-13 4:26am    
can post the output of profiler so that I can help you ?
Member 10423955 25-Nov-13 4:35am    
im not sure I understand

1 solution

Um.
How do you expect that to work?
SQL
@BookID int output
So you don't provide a value:
C#
SqlParameter parm = new SqlParameter("@BookID", SqlDbType.Int);
parm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm);
But then:
SQL
update dbo.Book
SET
   ...
Where  BookID = @BookID
No value means no matches...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900