Use button to update a single row in a database table

Last post 05-20-2008 8:39 AM by ahlaj77. 17 replies.

Sort Posts:

  • Surprise [:O] Use button to update a single row in a database table

    05-14-2008, 1:47 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    On my form I am wanting to hit the "submit button" which uses a stored procedure to update a single row in the database. I've been trying several things but haven't worked yet. Can't quite get it to work :-/ Here is what I have so far....Thanks for your help! ((using C#)) by the way

    **STORED PROCEDURE**
    Alter procedure dbo.BooksUpdate

    @BookName
    nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint Output

    As
    Set @BookID = 0

    -- update books table
    Update Books

    Set Books.BookName = @BookName,
          Books.BookName = @BookReview,
    Where Books.BookID = @BookID

    Set @BookID = @@Identity
    If @BookID Is Null Set @BookID = 0


    Return
    GO

    ***Call to the Database/Stored Procedure*** (bus obj)
    public class Books
    {
    private static string connBookUpdate = "Data Source=server1;Integrated Security=True;Initial Catalog=BookListings";
    private static long bookID = 0;

    public static long UpdateBooks(string bookName, string bookReview)
    {
    SqlParameter cmdParm;
    bookID = 0;
    using (SqlConnection connection = new SqlConnection(connBookUpdate))
    {
    using (SqlCommand command = new SqlCommand("BooksUpdate", connection))
    {
    command.CommandType =
    CommandType.StoredProcedure;
    command.Parameters.Add(
    new SqlParameter("@BookName", bookName));
    command.Parameters.Add(
    new SqlParameter("@BookReview",bookReview));
    // set return parameter

    cmdParm = command.Parameters.Add(new SqlParameter("@BookID", SqlDbType.BigInt));
    cmdParm.Direction =
    ParameterDirection.Output;

    connection.Open();
    command.ExecuteNonQuery();
    bookID = (
    long)command.Parameters["@BookID"].Value;
    }

    return bookID;

    ****SubmitBookUpdate page***

    protected void btnSubmitUpdate_Click(object sender, EventArgs e)
    {
    long bookID = 0;
    bookID =
    this.UpdatingBooks();
    Session.Clear();
    Response.Redirect(
    "BooksHome.aspx");
    }

    private long UpdatingBooks()
    {
    return Books.UpdateBooks(txtBookReview.Text, txtBookName.Text);
    }

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:21 PM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    What is the error or problem you get? 

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:29 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    When I hit submit it will redirect to the Home page (like it suppose to after it updates the table..ideally) but when i check my Books table in the database nothing gets updated. So i'm not sure whats going on :-/ don't kno if its my stored procedure or what

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:38 PM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    Oh yes now I see the problem:

    @BookName nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint Output

    As
    Set @BookID = 0

    -- update books table
    Update Books

    Set Books.BookName = @BookName,
          Books.BookName = @BookReview,
    Where Books.BookID = @BookID

    Set @BookID = @@Identity
    If @BookID Is Null Set @BookID = 0


    Return
    GO

    You try to update the datarow where the BookId = 0 everytime. You need to fill @BookId with a fitting BookId of the database. If you don't have one you have to make an insert statement

     

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:42 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    The BookID in the Books table is autogenerated (or i should say i set the Identity to yes when i was setting up the table). Dunno if that changes your answer or not :-/

     

    ~*~I like ur icon btw!~*~ Yes

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:48 PM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    Make the following changes:  

    @BookName nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint Output

    As
    Set @BookID = 0

    -- INSERT books table

    Insert into Books (BookName, BookReview) VALUES (@BookName, @BookReview) 

    Set @BookID = @@Identity

    If @BookID Is Null Set @BookID = 0

    Return
    GO

     You have to make an insert statement

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Use button to update a single row in a database table

    05-14-2008, 2:57 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    Big SmileIsn't that inserting a new row into the Books table? I'm just wanting to update an already existing row in the Books table Cool

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-14-2008, 3:01 PM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    Hey if you have no ID then you have no datarow then you need INSERT. IF YOU HAVE ID THEN PASS IT TO @BOOKID AND UPDATE IT.

     

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Use button to update a single row in a database table

    05-14-2008, 3:10 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    I am passing bookID as a session variable to the SubmitBookUpdate page....to display in a textbox...but not sure what u mean "pass it to the bookID" and update it..pass it in my .aspx page or stored procedure?

     BTW I appreciate ur help so far!! :-)

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-14-2008, 3:31 PM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 6:18 AM
    • Germany, Bavaria
    • Posts 251

    @BookName nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint Output

    As
    Set @BookID = 0

    -- update books table
    Update Books

    Set Books.BookName = @BookName,
          Books.BookName = @BookReview,
    Where Books.BookID = @BookID

    Set @BookID = @@Identity
    If @BookID Is Null Set @BookID = 0


    Return
    GO

     

    Look you say update where Bookid = @BookId. @BookId = 0 every time. So you need a filled parameter in the stored proc which is the correct id value for the fitting row. 

     

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Use button to update a single row in a database table

    05-14-2008, 3:41 PM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    One my bookshome.aspx page I have a gridview that has 3 columns (bookID, bookName, bookReview) and 4th column has a link button (shows up on each row) which when is clicked that particular ID is passed to the submitUpdatebook.aspx page. I think what i get ur saying about 'manually' typing in an ID in the stored procedure for it to update. But when you are choosing a particular row (or bookID) from the gridview..the book ID's change..that make sense? just wanted to further explain :)

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-15-2008, 8:28 AM
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    Tongue Tied i'm stuck and need some help :-/ please?

    "People will mainly remember you for who you are, not what you were a part of"
  • Re: Use button to update a single row in a database table

    05-15-2008, 8:48 AM
    Answer

    Hi Dude,

    Here is the new code -

    **STORED PROCEDURE**
    Alter procedure dbo.BooksUpdate

    @BookName
    nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint ---  Delete the Output 
    As
     
    ---Set @BookID = 0 - Delete this line

    -- update books table
    Update Books

    Set Books.BookName = @BookName,
          Books.BookName = @BookReview,
    Where Books.BookID = @BookID

    ---Set @BookID = @@Identity - Delete this line // @@identity returns the last generated identity - you have to use @@rowcount instead. Anyways you dont need it.
    ---If @BookID Is Null Set @BookID = 0
    // Dont need this line

    Return
    GO

    ***Call to the Database/Stored Procedure*** (bus obj)
    public class Books
    {
    private static string connBookUpdate = "Data Source=server1;Integrated Security=True;Initial Catalog=BookListings";
    private static long bookID = 0;

    public static long UpdateBooks(string bookName, string bookReview, long bookId)
    {
    SqlParameter cmdParm;
    using (SqlConnection connection = new SqlConnection(connBookUpdate))
    {
    using (SqlCommand command = new SqlCommand("BooksUpdate", connection))
    {
    command.CommandType =
    CommandType.StoredProcedure;
    command.Parameters.Add(
    new SqlParameter("@BookName", SqlDbType.Varchar, 100));
    command.Parameters.Add(
    new SqlParameter("@BookReview",SqlDbType.Varchar, 100));

    command.Parameters.Add(new SqlParameter("@BooId",SqlDbType.BigInt));

    connection.Open();
    return Convert.ToLong(command.ExecuteNonQuery());  // This returns number of rows updated.
     
    //bookID = (long)command.Parameters["@BookID"].Value; -- Delete this line

    Let me know if you need any further help. My idea was to explain the problem not to post correct code.


     

     

  • Wink [;)] Re: Use button to update a single row in a database table

    05-15-2008, 9:48 AM
    Answer
    • Loading...
    • ahlaj77
    • Joined on 03-18-2008, 2:15 PM
    • usa
    • Posts 98

    Below is my code once again but with your changes...let me kno if I read ur 'instructions' correctly..I bolded the sections that I corrected from your code..unless i had to delete them. THANK YOU!!! :)

    Alter procedure dbo.BooksUpdate

    @BookName nvarchar(10),
    @BookReview
    nvarchar(50),
    @BookID bigint

    As

    -- update books table
    Update Books

    Set Books.BookName = @BookName,
          Books.BookName = @BookReview,
    Where