Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
345
How to add a default value from code when doing inserts (add row)?
posted

I have 3 columns in my table: ProductId, CityName, ProductName. I did the following steps:

1. Add new webgrid to my page
2. From ultrawebgrid's design view, create a new SQLDataSource and pick Advanced - generate CRUD
3. Make hidden attribute TRUE for ProductId and CityName
4. Make sure DataKeyField is set to ProductId
5. Allow adds/updates

Inserts and updates all work fine except I want to add a default value for CityName from my code when adding new rows. Currently, it adds NULLs for CityName when adding rows. I have changed my InsertCommand in my TestPage.aspx.vb's PageLoad event as follows:

SqlDataSource.InsertCommand = "INSERT INTO Products ([CityName], [ProductName]) VALUES ('" & SessionCityVar & "', @ProductName)"

Am I missing something here? Strange thing is when I change the InsertCommand code in the aspx page by hardcoding a cityname value, it uses that value when doing inserts. When I change the insertcommand in codebehind, it doesn't work.

Parents
No Data
Reply
  • 2167
    Verified Answer
    posted

    check the order of events. the aspx is probably overring your codebehind depending on where you are setting the values.

    Try setting the values in the InitializeRow event.  I am guessing here I dont know the events off hand

    UltraWebGrid1.Bands(0).Columns(0).DefaultValue = "Click Me"

Children