views:

692

answers:

3
protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
        {
            cn.Open();
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.ExecuteNonQuery();
            cn.Close();
        }
        DetailsView1.DataBind();
    }
}

The Stored Procedure is working on SQL Server - Updating a column1 that's on the Form. but not showing the results/data on .net with NO Error.

A: 

It looks like the stored procedure is running, but you're not using any results from it that would cause it to refresh your DataSource. You would have to get something from the database after you ran the stored procedure to actually databind to.

An simple example of bringing the data back:

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd2))        
        {
            var dataSet = new DataSet();
            cn.Open();
            cmd2.CommandType = CommandType.StoredProcedure;
            adapter.Fill(dataSet);
            var _result = //get to your result some how.
            DetailsView.DataSource = result;
            cn.Close();
        }
        DetailsView1.DataBind();
    }
}

I really wouldn't recommend doing this. I'm only showing this to illustrate why I think your DetailsView isn't being refreshed with the data you're expecting.

Joseph
A: 

I'm just guessing here, but how about something like this:

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    using (SqlCommand cmd2 = new SqlCommand("uspUpdateDisplayHours", cn))
    {
        cmd2.CommandType = CommandType.StoredProcedure;
        new SqlDataAdapter(cmd2).Fill(ds);
    }
    DetailsView1.DataSource = ds;
    DetailsView1.DataBind();
    }
}

In the above example, I am assuming that you want to return something from the sp.


If you just want to update the DetailsView with data from the database (like when you populated the form when it loaded), just run that method again.

void PopulateForm() {
    //get data from database and bind it to the ListView
}

protected void DetailsView1_ItemInserted(object sender, EventArgs e)
{ 
    // <= here run the uspUpdateDisplayHours sp

    PopulateForm(); //run this method method again so that the data is refreshed        
}
Andreas Grech
You are right I want to return something from the sp. After update from the Form(.net) I updated data still not showing. But then when I go BACK to the Form(.net) it does show the updated data. It's not refreshing the Form(.net). help~
Kombucha
A: 

You are calling ExecuteNonQuery which should be used for updating the database Insert, Delete, Update.

Try using ExecuteReader which will return the results in a SqlDataReader object.

RedWolves
I tried ExecuteReader... After update from the Form(.net) the updated data still not showing. But then when I go BACK to the Form(.net) it does show the updated data. It's not refreshing the Form(.net). help~
Kombucha