[RESOLVED] autoincrement values in autogenerated datagridview-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] autoincrement values in autogenerated datagridview

  1. #1

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    Resolved [RESOLVED] autoincrement values in autogenerated datagridview

    hi pips. i have a datagridview that is autogenerated together with a binding navigator. however, the table that it is binded to has a primary key that is unique and has an auto incremented value. my problem is that when i add a record in the datagrid without putting a value for my primary key, it generates an error saying it doesnt allow a null value. how can i do it so that it will allow values since the database that i use(mysql) automatically creates a value for my primary key.. any ideas? please help.. thanks..

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: autoincrement values in autogenerated datagridview

    Hey,

    Can you show some of the code that you are using?

    What you are describing doesn't really make sense to me. When you do the insert to the database, you shouldn't need to provide a value for the primary key column, as like you said, this is generated by the database.

    I would suggest that this is an error in the DataSet definition that you are using?!?

    Gary

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,937

    Re: autoincrement values in autogenerated datagridview

    My guess is that you generated the DataTable like this:
    vb.net Code:
    1. myDataAdapter.Fill(myDataSet, "MyTable")
    correct? Did you set the MissingSchemaAction property of the DataAdapter to AddWithKey or did you leave it the default Add? If you left it the default then that explains the problem.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    Re: autoincrement values in autogenerated datagridview

    hello.. thanks for your replies.. what i am trying to do is that i have a mysql table.. which have several fields in it. including a primary key named SERIES that auto increments. so what i did is that i create a datasource from the Data Menu -> Add New Data Source. after that, i directly drag the datasource into my form and it automatically generates a datagridview and a bindingnavigator wherein i could manipulate the records. the problem is when i add a record and didnt place a value in the series field then it creates an error. it says the series field doesnt accept a null value. i dont want to place a value on the series field since i want the mysql database to generate it for me.. i hope i explained it fully for all of you to understand. i am still a newbie at c# so i am trying the auto code generation prowess of c#. is this a good thing? or it is still best to create your own code and not rely on the auto code generation of c#? please help.. thanks.

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: autoincrement values in autogenerated datagridview

    Hey,

    To get you started, there is nothing wrong with using the wizards to generate the code for you, but going forward, you are going to want to understand exactly what is going on under the hood, so that you can, if you need to, create your own code.

    Can you search your code for the snippet that jm has posted? Can you confirm that this is what is being used?

    Gary

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,937

    Re: autoincrement values in autogenerated datagridview

    When you create a Data Source, if the primary key column of a table is automatically generated by the database then the corresponding DataColumn in the corresponding DataTable should have its AutoIncrement, AutoIncrementSeed and AutoIncrementStep properties set. Why that hasn't happened in your case, I'm not sure but you can open the DataSet designer and set them yourself if need be.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: autoincrement values in autogenerated datagridview

    Hey,

    I haven't played with the MySql Wizards within Visual Studio, preferring to hand craft anything like that, but it could be a bug in the way the wizard works.

    Which version of the MySql connector are you using? The latest is 6.1.2.

    Gary

  8. #8

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    Re: autoincrement values in autogenerated datagridview

    gep13 and jmcilhinney, thank you so much for helping me. your absolutely right jmcilhinney, the AutoIncrement, AutoIncrementSeed and AutoIncrementStep properties are still not set. i dont know, maybe its because im not using the current version of mysql. i am still using v.5.0. i just set it manually and it works perfect. thanks again. i hope you wont get tired of helping newbies like me. tc.

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: [RESOLVED] autoincrement values in autogenerated datagridview

    Hey,

    I doubt that this would be an issue with your MySql Version, but rather the version of the Connector that you are using. Did you confirm which version you are using?

    Gary

  10. #10

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    Re: [RESOLVED] autoincrement values in autogenerated datagridview

    i am using mysql connector net 6.0.4.. i dont know why it wont automatically set the auto increment properties of a particular field in the table.

  11. #11
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: [RESOLVED] autoincrement values in autogenerated datagridview

    Hey,

    As I said, I don't use the MySql connector in the manner that you are, so I don't know if the later version fixes this issue or not. You might want to take a look at the releases notes, and/or simply install the new version.

    You have nothing to lose.

    Gary

  12. #12

    Thread Starter
    Lively Member druid's Avatar
    Join Date
    Jun 2006
    Posts
    81

    Re: [RESOLVED] autoincrement values in autogenerated datagridview

    ok. thanks..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.