Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Null parent row parameter for addRow method, using typed dataset

Null parent row parameter for addRow method, using typed dataset

Scheduled Pinned Locked Moved Database
helpcsharpdatabasevisual-studiotutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hoonzis
    wrote on last edited by
    #1

    Hello, We're developing this small helpdesk application in we run into this problem: I have a table of tickets and table of user, each ticket has in the database the field user_id where nulls are allowed. I'm using typed dataset generetad using the Visual Studio, so there is also the relation FK_users_tickets generated.So in my myDataSet.tickets table a method addTicketsRow() was created with several arguments, one of them being of type myDataSet.usersRow. I know that's the parent usersRow, so it's ok, the problem is when i want to add the new row with the user_id field null. How to provide usersRow parameter which is gona be null? Well ofcourse i tried providing the null parameter but im just getting nullreference exception. I also tried to find some constructor or method to create empty usersRow, but i found just the method myDataSet.users.newUsersRow(), but this one will just create another new usersRow and will set it's id, because there is autoincrement on the user_id field and obviously i don't want to do that. Looks litle confused, but hope the explanation is understoodable. Actually I'm almost sure it should work with the null parameter, because i had a piece of code like this before and that time it worked, but now i've already spend two days on solving and googling and still didn't come up with answear...so any help would be realy appriciated. thnx Honga

    M 1 Reply Last reply
    0
    • H hoonzis

      Hello, We're developing this small helpdesk application in we run into this problem: I have a table of tickets and table of user, each ticket has in the database the field user_id where nulls are allowed. I'm using typed dataset generetad using the Visual Studio, so there is also the relation FK_users_tickets generated.So in my myDataSet.tickets table a method addTicketsRow() was created with several arguments, one of them being of type myDataSet.usersRow. I know that's the parent usersRow, so it's ok, the problem is when i want to add the new row with the user_id field null. How to provide usersRow parameter which is gona be null? Well ofcourse i tried providing the null parameter but im just getting nullreference exception. I also tried to find some constructor or method to create empty usersRow, but i found just the method myDataSet.users.newUsersRow(), but this one will just create another new usersRow and will set it's id, because there is autoincrement on the user_id field and obviously i don't want to do that. Looks litle confused, but hope the explanation is understoodable. Actually I'm almost sure it should work with the null parameter, because i had a piece of code like this before and that time it worked, but now i've already spend two days on solving and googling and still didn't come up with answear...so any help would be realy appriciated. thnx Honga

      M Offline
      M Offline
      Mark J Miller
      wrote on last edited by
      #2

      You have to ask yourself, "what is the reason for having the ForeignKeyConstraint"? The purpose of a foreign key constraint is to enforce the data integrity of the relationship. Depending on what it is you're trying to accomplish here's some suggestions: 1) Set the EnforceConstraints property of the Dataset to false. However, this will affect all your constraints, not just the specific foreign key constraint. So if you have other constraints you need to be aware of what the affect will be of turning them off. 2) Use a DataRelation instead. The trick is that it will add a foreign key constraint which you will have to remove. This will work if the purpose of your constraint is simply to indicate a relation between the tables and not to enforce data integrity. Then you can use methods like DataRow.GetChildRows(). 3) If you don't want to enforce data entegrity and you aren't using methods like DataRow.GetChildRows() then you may not need the constraint at all

      H 2 Replies Last reply
      0
      • M Mark J Miller

        You have to ask yourself, "what is the reason for having the ForeignKeyConstraint"? The purpose of a foreign key constraint is to enforce the data integrity of the relationship. Depending on what it is you're trying to accomplish here's some suggestions: 1) Set the EnforceConstraints property of the Dataset to false. However, this will affect all your constraints, not just the specific foreign key constraint. So if you have other constraints you need to be aware of what the affect will be of turning them off. 2) Use a DataRelation instead. The trick is that it will add a foreign key constraint which you will have to remove. This will work if the purpose of your constraint is simply to indicate a relation between the tables and not to enforce data integrity. Then you can use methods like DataRow.GetChildRows(). 3) If you don't want to enforce data entegrity and you aren't using methods like DataRow.GetChildRows() then you may not need the constraint at all

        H Offline
        H Offline
        hoonzis
        wrote on last edited by
        #3

        Thanks for answer, the DataSet is generated from the database using the VisualStudio so there is a DataRelation between those 2 tables already. So I can simply erase this relation and I guess it is gonna work fine after that. The problem that I don't understand,is however, little more general. The generated addRow method parameters looks something like this: ticketsTable.addTicketsRow(DateTime ticket_date,string ticket_title,....,**MyDataSet.usersRow user_id**) and I know that in the database running on SQL Server, the field user_id in table tickets allows Nulls(and also in the generated DataSet). So when Visual Studio created the DataSet for me with this wonderful method, what parameter should I supply to this method for the usersRow, so the field in the database would be Null? When I supply null, which was my first idea, I just get "object reference not set to instance..." exception. Honga

        1 Reply Last reply
        0
        • M Mark J Miller

          You have to ask yourself, "what is the reason for having the ForeignKeyConstraint"? The purpose of a foreign key constraint is to enforce the data integrity of the relationship. Depending on what it is you're trying to accomplish here's some suggestions: 1) Set the EnforceConstraints property of the Dataset to false. However, this will affect all your constraints, not just the specific foreign key constraint. So if you have other constraints you need to be aware of what the affect will be of turning them off. 2) Use a DataRelation instead. The trick is that it will add a foreign key constraint which you will have to remove. This will work if the purpose of your constraint is simply to indicate a relation between the tables and not to enforce data integrity. Then you can use methods like DataRow.GetChildRows(). 3) If you don't want to enforce data entegrity and you aren't using methods like DataRow.GetChildRows() then you may not need the constraint at all

          H Offline
          H Offline
          hoonzis
          wrote on last edited by
          #4

          Thanks for your answer, The DataSet is generated from Visual Studion from SQL Server so there already is a DataRelation between those 2 tables "tickets" and "users". So I guess that when I delete this Relation it is gonna work fine. The problem I don't understand is however more general. My addRow method on table "tickets" looks something like this: myDataSet.ticketsTable.addTicketsRow(string ticket_title,DateTime ticket_created,...,**MyDataSet.usersRow parentusersRowbyFK_ticket_users**) In the DataSet the FK_tickets_users is set to "Relation only". In the database there is field user_id of type int and this field I'm setting by the MyDataSet.usersRow parentusersRowbyFK_ticket_users parameter. This field user_id allows Nulls. So my question is what parameter should I provide to the addTicketsRow method so the field user_id in the database would be Null? As I said before, null was my first try, but than I'm getting NullReferenceObjectException.

          M 1 Reply Last reply
          0
          • H hoonzis

            Thanks for your answer, The DataSet is generated from Visual Studion from SQL Server so there already is a DataRelation between those 2 tables "tickets" and "users". So I guess that when I delete this Relation it is gonna work fine. The problem I don't understand is however more general. My addRow method on table "tickets" looks something like this: myDataSet.ticketsTable.addTicketsRow(string ticket_title,DateTime ticket_created,...,**MyDataSet.usersRow parentusersRowbyFK_ticket_users**) In the DataSet the FK_tickets_users is set to "Relation only". In the database there is field user_id of type int and this field I'm setting by the MyDataSet.usersRow parentusersRowbyFK_ticket_users parameter. This field user_id allows Nulls. So my question is what parameter should I provide to the addTicketsRow method so the field user_id in the database would be Null? As I said before, null was my first try, but than I'm getting NullReferenceObjectException.

            M Offline
            M Offline
            Mark J Miller
            wrote on last edited by
            #5

            What I'd recommend is creating a new class which inherits from the generated dataset so you can add an overloaded method which does not contain the parameters which are causing you null reference exceptions. Your inherited class will have direct access to the table object itself which will allow you direct access to the table object so you can add a row w/ null values.

            H 1 Reply Last reply
            0
            • M Mark J Miller

              What I'd recommend is creating a new class which inherits from the generated dataset so you can add an overloaded method which does not contain the parameters which are causing you null reference exceptions. Your inherited class will have direct access to the table object itself which will allow you direct access to the table object so you can add a row w/ null values.

              H Offline
              H Offline
              hoonzis
              wrote on last edited by
              #6

              Thank you, we solved it yesterday similary to the way that you suggested. We just didn't create new class inheriting from DataSet but we added the new method without the problem parameter to our class which is providing some other data operations. Honga

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups