update the database using dataadpater in asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muskan
    New Member
    • Nov 2006
    • 35

    update the database using dataadpater in asp.net

    hello,

    I have written this code on button_click event.
    but it shows "Object reference not set to an instance of an object" error.
    can u help to solve this error.

    String conn= "data source= VTECHS; database= bc;uid=greymoun t;password= manish2809";
    String str= "select * from details";
    SqlDataAdapter da= new SqlDataAdapter( str,conn);
    SqlCommandBuild er commb=new SqlCommandBuild er(da);
    DataSet ds =new DataSet();
    da.Fill(ds);
    DataTable dt=new DataTable();
    dt=ds.Tables["details"];
    dataRow rownew= ds.Tables["details"].NewRow();
    rownew["name"]=txtLcode.Text ;
    ds.Tables["details"].Rows.Add(rowne w);
    da.Update(ds);
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Have u opened the connection?
    because SqlDataAdapter da= new SqlDataAdapter( str,conn);
    Here con is your connectionstrin g .So yuo might be getting error there

    Shweta

    Comment

    • muskan
      New Member
      • Nov 2006
      • 35

      #3
      Thanks for reply........
      but i have opened the connection in the code.

      I have written like tht but it is still not working.


      SqlConnection conn= new SqlConnection ("data source= VTECHS; database= abc;uid=greymou nt;password= manish2809");
      conn.Open();

      SqlDataAdapter da= new SqlDataAdapter( new SqlCommand("sel ect * from details",conn)) ;

      Please help me to solve this query.

      Comment

      • radcaesar
        Recognized Expert Contributor
        • Sep 2006
        • 759

        #4
        Why did u use the command builder there ?

        Remove the command builder.

        Also, when u create a Datase, it has a Data Table.

        U can access it directly by ds.Tables[0]

        Try to reduce the code and finish it.

        :)

        Comment

        • vinaykeshav
          New Member
          • Dec 2006
          • 25

          #5
          check if the database already exists for which your trying to access from code.
          also debug and find out where exactly its breaking...

          Comment

          • muskan
            New Member
            • Nov 2006
            • 35

            #6
            thanks for reply.

            actually I want to insert the new record into the table.

            Comment

            • muskan
              New Member
              • Nov 2006
              • 35

              #7
              when I debug, error is showing in this line

              dataRow rownew= ds.Tables["details"].NewRow();

              Comment

              • vinaykeshav
                New Member
                • Dec 2006
                • 25

                #8
                dataRow rownew= ds.Tables["details"].NewRow();[/QUOTE]

                make the above line

                DataRow rownew= ds.Tables["details"].NewRow();[/QUOTE]
                the datarow should b in caps..
                and plz check u have this at the top
                using System.Data;
                using System.Data.Sql Client;

                Comment

                • muskan
                  New Member
                  • Nov 2006
                  • 35

                  #9
                  thanks......

                  my problem is solved.

                  now I have used ds.Tables[0].
                  but command builder is necessary becoz I want to insert new record into table
                  & it provides many other useful commands to dataadapter such as insert command which is useful for adding the new record into database.




                  Originally posted by radcaesar
                  Why did u use the command builder there ?

                  Remove the command builder.

                  Also, when u create a Datase, it has a Data Table.

                  U can access it directly by ds.Tables[0]

                  Try to reduce the code and finish it.

                  :)

                  Comment

                  • vinaykeshav
                    New Member
                    • Dec 2006
                    • 25

                    #10
                    DataTable dt =new DataTable();
                    dt=ds.Tables["details"];

                    //ds.Tables["details"].NewRow();
                    dt.NewRow();

                    that was the gud soln to the probs i thought u could have done this..
                    thats taking new row from the datatable instead of ds..

                    Comment

                    • chauhanavneesh
                      New Member
                      • Feb 2007
                      • 1

                      #11
                      Originally posted by muskan
                      hello,

                      I have written this code on button_click event.
                      but it shows "Object reference not set to an instance of an object" error.
                      can u help to solve this error.

                      String conn= "data source= VTECHS; database= bc;uid=greymoun t;password= manish2809";
                      String str= "select * from details";
                      SqlDataAdapter da= new SqlDataAdapter( str,conn);
                      SqlCommandBuild er commb=new SqlCommandBuild er(da);
                      DataSet ds =new DataSet();
                      da.Fill(ds);
                      DataTable dt=new DataTable();
                      dt=ds.Tables["details"];
                      dataRow rownew= ds.Tables["details"].NewRow();
                      rownew["name"]=txtLcode.Text ;
                      ds.Tables["details"].Rows.Add(rowne w);
                      da.Update(ds);


                      Its give me ==>a syntax error in insert into statement.
                      please solve my problem

                      Comment

                      Working...