0

So this is my Application_End C# code used to update the database table with values from an array:

protected void Application_End(object sender, EventArgs e)
    {
        string conStr = Session["ConnectionString"].ToString();
        string CMDDDStr = "SELECT * FROM Portfolio";

        bool continueFlag = true; //use that somehow

        if (continueFlag)
        {
            SqlDataAdapter daa = new SqlDataAdapter(CMDDDStr, conStr);
            DataSet dss = new DataSet();
            daa.Fill(dss);
            for (int i = 0; i < dss.Tables[0].Rows.Count - 4; i++)
            {
                int[] services = (int[])Application["services"];
                int index0 = services[i];
                dss.Tables[0].Rows[i][i+4] = index0;
                //// Create connected scenario connection
            }
            SqlCommandBuilder builder = new SqlCommandBuilder(daa);
            daa.UpdateCommand = builder.GetUpdateCommand();
            daa.Update(dss);
        }
    }

And this is the SQL of my database table:

CREATE TABLE [dbo].[Portfolio] (
[Description] NVARCHAR (MAX)  NULL,
[ImageData]   VARBINARY (MAX) NULL,
[delete]      NVARCHAR (50)   NULL,
[id]          INT             IDENTITY (1, 1) NOT NULL,
[service0]    INT             NULL,
[service1]    INT             NULL,
[service2]    INT             NULL,
[service3]    INT             NULL,
[service4]    INT             NULL,
PRIMARY KEY CLUSTERED ([id] ASC)
);

Problem is... This doesn't do anything. It doesn't update the database - any ideas?

(the problem is with the code)

kfir ezer
  • 159
  • 1
  • 11
  • The last comment in [this answer](https://stackoverflow.com/a/4544666/3034273) probably explains what you're seeing. `Application_End` isn't called if the application crashes or is forcibly closed – Xerillio May 18 '20 at 17:28
  • 1
    Add to that: making calls to a database is probably not the best idea in the method that's supposed to close down your application. – Xerillio May 18 '20 at 17:29
  • @Xerillio I am aware that it's not the best idea, its a task from school so I have to do it like this... what can I use so that ANY time I close visual studio (forcibly - pressing on the X button) that code will run? – kfir ezer May 18 '20 at 17:45
  • I do not think you need following instruction which may be causing the issue : daa.UpdateCommand = builder.GetUpdateCommand(); The new SqlCommandBuilder(daa); is already building the Update Command and it looks like you may be killing the one that was automatically generated. – jdweng May 18 '20 at 17:51
  • @jdweng still doesn't work without it... – kfir ezer May 18 '20 at 18:03
  • I'm trying the code on its own with a button (not in Application_End) with a debugger and I genuinely don't have a clue where the problem is :( – kfir ezer May 18 '20 at 18:07
  • If you are not getting an exception you are probably updating the a different database (may be on a different server). Check the SQL log files to see if you are actually connecting. A SQL Server database is a MDF file that may be attached to a database or may be a local database. So you are probably updating the wrong MDF file. Most likely there are issues with the connection string. – jdweng May 18 '20 at 18:16
  • @jdweng I used `UPDATE` to update most the tables, but the school assignment forces me to use Application & DataDet. I did use datasets to update 1 time in my site, and it looked like this `int[] services = (int[])Application["services"]; int index0 = services[i]; dss.Tables[0].Rows[i][i+4] = index0;` & worked – kfir ezer May 18 '20 at 18:42
  • I don't get it, why is this code flawed :""""D – kfir ezer May 18 '20 at 18:55
  • I SOLVED IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! – kfir ezer May 18 '20 at 18:56

1 Answers1

0
string conaaaStr = Session["ConnectionString"].ToString();
        string CMDDDStr = "SELECT * FROM Portfolio";

        bool continuaeFlag = true; //use that somehow

        if (continuaeFlag)
        {
            SqlDataAdapter daa = new SqlDataAdapter(CMDDDStr, conaaaStr);
            DataSet dss = new DataSet();
            daa.Fill(dss);
            for (int i = 0; i < dss.Tables[0].Columns.Count - 4; i++)
            {
                int[] services = (int[])Application["services"];
                int index0 = services[i];
                dss.Tables[0].Rows[0][i + 4] = index0;
                //// Create connected scenario connection
            }
            SqlCommandBuilder builder = new SqlCommandBuilder(daa);
            builder.GetUpdateCommand();
            daa.Update(dss);
        }
kfir ezer
  • 159
  • 1
  • 11
  • The line builder.GetUpdateCommand(); is doing nothing. The changing row to columns makes no sense : for (int i = 0; i < dss.Tables[0].Columns.Count - 4; i++). Yes for some reason the database is changing not due to any thing you did,. – jdweng May 18 '20 at 21:28
  • @jdweng it does. the database has 5 columns that I run the loop on - service0 through 5. by running it on rows, you are running the loop on your table data. but by running it on the columns, it is actually tracking the number of services available and running based on that. an added benefit of this is the dynamicity. – kfir ezer May 20 '20 at 06:41