none
WPF not changing the database RRS feed

  • Question

  • Hello, I'm building a little application in WPF for the first time, normaly a webbdeveloper, and I ran into a problem. I'm havinga form with it's help I want to add new information to the database I have behind the app.

    Everything seems to work find when I run the app, all the code runs through and I get the feedback that my data have been added, I can even see it in the front end after calling for it. But when I manually go in to the database and look for it, I can't find it. If I close the app and runs it again after a little while, the "added data" is gone again. I'm sure I'm missing something stupid little thing that is diffrent from my normal code. I was hopping someone could point me in the right direction. I add a few snippets from my code.

     public bool add(string name)
            {
                if (find(name)) {return true;}

                // Get the setuped Db-comand object
                DbCommand comm = GenericDataAccess.CreateCommand();
                // Set the name of the stored procedure
                comm.CommandText = "addToItemGroup";
                // Create new parameter
                DbParameter param = comm.CreateParameter();
                param.ParameterName = "@Name";
                param.Value = name;
                param.DbType = DbType.String;
                comm.Parameters.Add(param);
                // returns true in case of success or false in case of an error
                try
                {
                    // execute the stored procedure and return true if it executes
                    // successfully, or false otherwise
                    return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
                }
                catch
                {
                    // prevent the exception from propagating, but return false to
                    // signal the error
                    return false;
                }

     // Creates and prepares a new DbCommand object on a new connection
        public static DbCommand CreateCommand()
        {
            // Obtain the database provider name
            string dataProviderName = Configuration.DbProviderName;
            // Obtain the batabase connection string
            string connectionString = Configuration.DbConnectionString;
            // Create a new data provider factory
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            // Obtain a database specific connection object
            DbConnection conn = factory.CreateConnection();
            // Set the connection string
            conn.ConnectionString = connectionString;
            // Create a database specific command object
            DbCommand comm = conn.CreateCommand();
            // Set the command type to stored procedure
            comm.CommandType = CommandType.StoredProcedure;
            // Return the initialized command object
            return comm;
        }

     public static int ExecuteNonQuery(DbCommand command)
        {
            // The number of affected rows
            int affectedRows = -1;
            // Execute the command making sure the connection gets closed in the end
            try
            {
                // Open the connection of the command
                command.Connection.Open();
                // Execute the command and get the number of affected rows
                affectedRows = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                // Log eventual errors and rethrow them
                Utilities.LogError(ex);
                throw ex;
            }
            finally
            {
                // Close the connection
                command.Connection.Close();
            }
            // return the number of affected rows
            return affectedRows;
        }

     

    I know the code have worked fine in my webbaplications, and is a standard in my own libary but guess something else is needed.

    Thanks in advance!

     

    • Moved by Sheldon _Xiao Monday, January 31, 2011 5:14 AM more appropriate (From:Windows Presentation Foundation (WPF))
    Thursday, January 27, 2011 4:57 PM

Answers

All replies

  • What is the value of affectedRows variable after you've executed command.ExecuteNonQuery()? What is the database? You're calling stored procedure, maybe you sould add commit statement in it? Maybe autocomit isn't turned on?

    Please provide additional details about your scenario


    God bless you!
    Thursday, January 27, 2011 7:38 PM
  • AffectedRows has the value 1 after command.ExecuteNonQuery() is executed. I'm using a SQL database, .mdf format. As far as autocomit goes I thought it was standard that it was on? If not, where do I need to look and how do I change it if needed?

     

    As off now the sp for the code above is

    ALTER PROCEDURE addToItemGroup
    @Name varchar(50)

    AS
    INSERT INTO ItemGroups
    (Name)
    VALUES
    (@Name)

     

     

    Friday, January 28, 2011 1:37 PM
  • Hello ALeara,

     

    Welcome to the MSDN Forum.

    According to your description, I think the reason is that you are updating data only in memory not submit the changes to the database.

    Look at this DataSet FAQ, I think it's helpful to you.

    Thanks for your support!
    Happy Chinese New Year!

    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 2, 2011 2:29 AM
    Moderator