I have a gridview.
- I’m adding values with parameters->open connection->execute
reader->connection close
But the problem is that the values added are not displayed in the gridview and if I reload the page I still can’t see them but if I click on the url textbox in the browser and press enter then I see the values added + 2/3 times the same values in other rows ( because of reload ).
string cstring = "connection string";
string query = "Insert into table (Name) values (@Name)";
Sql conection conect = new sqlconnection(cstring);
sqlcommand command = new sqlcommand(query,conect) ;
command.parameters.addwithvalue("Name",name.text);
conect.open();
comanda.executeReader();
conect.close
I’ve run into this issue many times. In general, I’ve disabled the GridView’s ViewState to fix the problem.
Explanation: On PostBack, GridView sees that it didn’t update any data (though some other part of your code did), so it doesn’t bother re-querying the database. Instead, it just reuses data from its ViewState.
Keep in mind that it is best to use GridView to update the DB rather than some other mechanism, and that is why you are encountering this problem.