Insert a record in sql database using vb.net datatable and datarow features











up vote
0
down vote

favorite












I am trying to insert a record in sql database using vb.net dataadapter, datatable, and datarow features. I use the following code but it gives me an error:



Object reference not set to an instance of an object



Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cn As New SqlConnection("Data Source=.SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
Dim da As New SqlDataAdapter
Dim ds As New DataSet

Try
cn.Open()
da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
da.Fill(ds)
Dim dt As New DataTable
dt = ds.Tables("emp_tbl")

'Error in this line(Object reference not set to an instance of an object)'
Dim dr As DataRow = dt.NewRow()

dr.Item("emp_id") = TextBox1.Text.Trim
dr.Item("emp_name") = TextBox2.Text.Trim
dr.Item("salary") = TextBox3.Text.Trim
dr.Item("age") = TextBox4.Text.Trim
dr.Item("emp_group") = TextBox5.Text.Trim
dt.Rows.Add(dr)
da.Update(ds)
MsgBox("Record Successfully Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class









share|improve this question
























  • check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
    – andy
    Feb 11 '13 at 5:23










  • NO. emp_tbl is empty.
    – Pallab Pal
    Feb 11 '13 at 6:11










  • If table is empty how do I insert a record using datatable an datarow?
    – Pallab Pal
    Feb 11 '13 at 6:15












  • If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
    – andy
    Feb 11 '13 at 6:22






  • 1




    I recommend looking into LINQ to SQL or Entity Framework
    – Brian Webster
    Feb 11 '13 at 6:40















up vote
0
down vote

favorite












I am trying to insert a record in sql database using vb.net dataadapter, datatable, and datarow features. I use the following code but it gives me an error:



Object reference not set to an instance of an object



Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cn As New SqlConnection("Data Source=.SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
Dim da As New SqlDataAdapter
Dim ds As New DataSet

Try
cn.Open()
da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
da.Fill(ds)
Dim dt As New DataTable
dt = ds.Tables("emp_tbl")

'Error in this line(Object reference not set to an instance of an object)'
Dim dr As DataRow = dt.NewRow()

dr.Item("emp_id") = TextBox1.Text.Trim
dr.Item("emp_name") = TextBox2.Text.Trim
dr.Item("salary") = TextBox3.Text.Trim
dr.Item("age") = TextBox4.Text.Trim
dr.Item("emp_group") = TextBox5.Text.Trim
dt.Rows.Add(dr)
da.Update(ds)
MsgBox("Record Successfully Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class









share|improve this question
























  • check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
    – andy
    Feb 11 '13 at 5:23










  • NO. emp_tbl is empty.
    – Pallab Pal
    Feb 11 '13 at 6:11










  • If table is empty how do I insert a record using datatable an datarow?
    – Pallab Pal
    Feb 11 '13 at 6:15












  • If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
    – andy
    Feb 11 '13 at 6:22






  • 1




    I recommend looking into LINQ to SQL or Entity Framework
    – Brian Webster
    Feb 11 '13 at 6:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to insert a record in sql database using vb.net dataadapter, datatable, and datarow features. I use the following code but it gives me an error:



Object reference not set to an instance of an object



Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cn As New SqlConnection("Data Source=.SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
Dim da As New SqlDataAdapter
Dim ds As New DataSet

Try
cn.Open()
da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
da.Fill(ds)
Dim dt As New DataTable
dt = ds.Tables("emp_tbl")

'Error in this line(Object reference not set to an instance of an object)'
Dim dr As DataRow = dt.NewRow()

dr.Item("emp_id") = TextBox1.Text.Trim
dr.Item("emp_name") = TextBox2.Text.Trim
dr.Item("salary") = TextBox3.Text.Trim
dr.Item("age") = TextBox4.Text.Trim
dr.Item("emp_group") = TextBox5.Text.Trim
dt.Rows.Add(dr)
da.Update(ds)
MsgBox("Record Successfully Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class









share|improve this question















I am trying to insert a record in sql database using vb.net dataadapter, datatable, and datarow features. I use the following code but it gives me an error:



Object reference not set to an instance of an object



Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cn As New SqlConnection("Data Source=.SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
Dim da As New SqlDataAdapter
Dim ds As New DataSet

Try
cn.Open()
da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
da.Fill(ds)
Dim dt As New DataTable
dt = ds.Tables("emp_tbl")

'Error in this line(Object reference not set to an instance of an object)'
Dim dr As DataRow = dt.NewRow()

dr.Item("emp_id") = TextBox1.Text.Trim
dr.Item("emp_name") = TextBox2.Text.Trim
dr.Item("salary") = TextBox3.Text.Trim
dr.Item("age") = TextBox4.Text.Trim
dr.Item("emp_group") = TextBox5.Text.Trim
dt.Rows.Add(dr)
da.Update(ds)
MsgBox("Record Successfully Inserted")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class






sql vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 '13 at 6:39









Brian Webster

20.3k39129208




20.3k39129208










asked Feb 11 '13 at 5:05









Pallab Pal

1124




1124












  • check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
    – andy
    Feb 11 '13 at 5:23










  • NO. emp_tbl is empty.
    – Pallab Pal
    Feb 11 '13 at 6:11










  • If table is empty how do I insert a record using datatable an datarow?
    – Pallab Pal
    Feb 11 '13 at 6:15












  • If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
    – andy
    Feb 11 '13 at 6:22






  • 1




    I recommend looking into LINQ to SQL or Entity Framework
    – Brian Webster
    Feb 11 '13 at 6:40


















  • check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
    – andy
    Feb 11 '13 at 5:23










  • NO. emp_tbl is empty.
    – Pallab Pal
    Feb 11 '13 at 6:11










  • If table is empty how do I insert a record using datatable an datarow?
    – Pallab Pal
    Feb 11 '13 at 6:15












  • If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
    – andy
    Feb 11 '13 at 6:22






  • 1




    I recommend looking into LINQ to SQL or Entity Framework
    – Brian Webster
    Feb 11 '13 at 6:40
















check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
– andy
Feb 11 '13 at 5:23




check "dt" ................ Whether 'ds.Tables("emp_tbl")' this has contains a rows ?
– andy
Feb 11 '13 at 5:23












NO. emp_tbl is empty.
– Pallab Pal
Feb 11 '13 at 6:11




NO. emp_tbl is empty.
– Pallab Pal
Feb 11 '13 at 6:11












If table is empty how do I insert a record using datatable an datarow?
– Pallab Pal
Feb 11 '13 at 6:15






If table is empty how do I insert a record using datatable an datarow?
– Pallab Pal
Feb 11 '13 at 6:15














If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
– andy
Feb 11 '13 at 6:22




If table is empty its possible to add new rows......... Check whether columns are displaying for that table or not , if no then there is a problem in your query(or database).
– andy
Feb 11 '13 at 6:22




1




1




I recommend looking into LINQ to SQL or Entity Framework
– Brian Webster
Feb 11 '13 at 6:40




I recommend looking into LINQ to SQL or Entity Framework
– Brian Webster
Feb 11 '13 at 6:40












4 Answers
4






active

oldest

votes

















up vote
0
down vote













check this out : Dim dr As DataRow = new dt.NewRow()






share|improve this answer

















  • 2




    I checked it out. It doesn't compile.
    – LarsTech
    May 24 '13 at 14:50


















up vote
0
down vote













You have done everything good but change the following line:



da.Update(ds)


As following:



Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
ESCBuilder.GetUpdateCommand()
da.UpdateCommand = ESCBuilder.GetUpdateCommand()
da.Update(ds)





share|improve this answer





















  • This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
    – Eldar Zeynalov
    Dec 11 '13 at 14:13


















up vote
0
down vote













Based on the feedback here and elsewhere, the following code worked for me:



    TestDataSet.GetChanges()
testTableAdapter.Fill(TestDataSet.log_test)
log_testDataGridView.Refresh()


What I needed to do was, create a new row, and go get the next value for the Primary Key(VARCHAR, NOT INT, because revisions got an "R" appended to the PK...not my rule...the rule of the company).



I wanted to put the refresh as close to the getting of the PK, which I had last after assigning values to the new datarow so it would get the latest Max +1.



So, I put the above code just before looking up the PK, and after assigning values to the datarow, other than PK. The code above caused the datarow to blank out. So, I put the above code just prior to the creation of the new DataRow.



For my code, this caused the code to get the latest data from the SQL table, then add the new datarow, and finally determine the last PK. Because the data used to populate the datarow is off my form, and there is no caclulations, the code runs fast enough for my needs. I suspect, if the connection to my database was slow, and/or, the number of people running the same process were substantial, I would have errors, such as duplicate PKs.



My answer to that would be to assign the datarow field values to variables, run the refresh, then assign the variables to the fields and save immediately.



Perhaps another way would be to get the new PK, then save an empty record, and then fill the record, except that enough of the fields in my table are REQUIRED so I might as well not try creating a blank record first.






share|improve this answer




























    up vote
    0
    down vote













    Imports System.Data.SqlClient
    Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
    con.Open()
    ‘ cmd = New SqlCommand("select * from Doctor", con)
    str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
    cmd = New SqlCommand(str, con)
    count = cmd.ExecuteNonQuery()
    MessageBox.Show(count & " Record inserted")

    con.close()
    End Sub

    Imports System.Data.SqlClient
    Public Class Form4
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim str As String
    Dim count As Integer
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
    con.Open()
    cmd = New SqlCommand("select * from Patient", con)
    cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
    cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
    cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
    cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
    ‘you can take any row in your program
    count = cmd.ExecuteNonQuery()
    MessageBox.Show("Record Deleted")
    End Sub





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14806190%2finsert-a-record-in-sql-database-using-vb-net-datatable-and-datarow-features%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      check this out : Dim dr As DataRow = new dt.NewRow()






      share|improve this answer

















      • 2




        I checked it out. It doesn't compile.
        – LarsTech
        May 24 '13 at 14:50















      up vote
      0
      down vote













      check this out : Dim dr As DataRow = new dt.NewRow()






      share|improve this answer

















      • 2




        I checked it out. It doesn't compile.
        – LarsTech
        May 24 '13 at 14:50













      up vote
      0
      down vote










      up vote
      0
      down vote









      check this out : Dim dr As DataRow = new dt.NewRow()






      share|improve this answer












      check this out : Dim dr As DataRow = new dt.NewRow()







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered May 24 '13 at 14:33









      user2417929

      11




      11








      • 2




        I checked it out. It doesn't compile.
        – LarsTech
        May 24 '13 at 14:50














      • 2




        I checked it out. It doesn't compile.
        – LarsTech
        May 24 '13 at 14:50








      2




      2




      I checked it out. It doesn't compile.
      – LarsTech
      May 24 '13 at 14:50




      I checked it out. It doesn't compile.
      – LarsTech
      May 24 '13 at 14:50












      up vote
      0
      down vote













      You have done everything good but change the following line:



      da.Update(ds)


      As following:



      Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
      ESCBuilder.GetUpdateCommand()
      da.UpdateCommand = ESCBuilder.GetUpdateCommand()
      da.Update(ds)





      share|improve this answer





















      • This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
        – Eldar Zeynalov
        Dec 11 '13 at 14:13















      up vote
      0
      down vote













      You have done everything good but change the following line:



      da.Update(ds)


      As following:



      Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
      ESCBuilder.GetUpdateCommand()
      da.UpdateCommand = ESCBuilder.GetUpdateCommand()
      da.Update(ds)





      share|improve this answer





















      • This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
        – Eldar Zeynalov
        Dec 11 '13 at 14:13













      up vote
      0
      down vote










      up vote
      0
      down vote









      You have done everything good but change the following line:



      da.Update(ds)


      As following:



      Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
      ESCBuilder.GetUpdateCommand()
      da.UpdateCommand = ESCBuilder.GetUpdateCommand()
      da.Update(ds)





      share|improve this answer












      You have done everything good but change the following line:



      da.Update(ds)


      As following:



      Dim ESCBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
      ESCBuilder.GetUpdateCommand()
      da.UpdateCommand = ESCBuilder.GetUpdateCommand()
      da.Update(ds)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 11 '13 at 14:11









      Eldar Zeynalov

      191113




      191113












      • This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
        – Eldar Zeynalov
        Dec 11 '13 at 14:13


















      • This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
        – Eldar Zeynalov
        Dec 11 '13 at 14:13
















      This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
      – Eldar Zeynalov
      Dec 11 '13 at 14:13




      This is and official update command but remember it work only when you have the primary key on the target table. If you don't have PK, the update commanndbuilders update command will not work
      – Eldar Zeynalov
      Dec 11 '13 at 14:13










      up vote
      0
      down vote













      Based on the feedback here and elsewhere, the following code worked for me:



          TestDataSet.GetChanges()
      testTableAdapter.Fill(TestDataSet.log_test)
      log_testDataGridView.Refresh()


      What I needed to do was, create a new row, and go get the next value for the Primary Key(VARCHAR, NOT INT, because revisions got an "R" appended to the PK...not my rule...the rule of the company).



      I wanted to put the refresh as close to the getting of the PK, which I had last after assigning values to the new datarow so it would get the latest Max +1.



      So, I put the above code just before looking up the PK, and after assigning values to the datarow, other than PK. The code above caused the datarow to blank out. So, I put the above code just prior to the creation of the new DataRow.



      For my code, this caused the code to get the latest data from the SQL table, then add the new datarow, and finally determine the last PK. Because the data used to populate the datarow is off my form, and there is no caclulations, the code runs fast enough for my needs. I suspect, if the connection to my database was slow, and/or, the number of people running the same process were substantial, I would have errors, such as duplicate PKs.



      My answer to that would be to assign the datarow field values to variables, run the refresh, then assign the variables to the fields and save immediately.



      Perhaps another way would be to get the new PK, then save an empty record, and then fill the record, except that enough of the fields in my table are REQUIRED so I might as well not try creating a blank record first.






      share|improve this answer

























        up vote
        0
        down vote













        Based on the feedback here and elsewhere, the following code worked for me:



            TestDataSet.GetChanges()
        testTableAdapter.Fill(TestDataSet.log_test)
        log_testDataGridView.Refresh()


        What I needed to do was, create a new row, and go get the next value for the Primary Key(VARCHAR, NOT INT, because revisions got an "R" appended to the PK...not my rule...the rule of the company).



        I wanted to put the refresh as close to the getting of the PK, which I had last after assigning values to the new datarow so it would get the latest Max +1.



        So, I put the above code just before looking up the PK, and after assigning values to the datarow, other than PK. The code above caused the datarow to blank out. So, I put the above code just prior to the creation of the new DataRow.



        For my code, this caused the code to get the latest data from the SQL table, then add the new datarow, and finally determine the last PK. Because the data used to populate the datarow is off my form, and there is no caclulations, the code runs fast enough for my needs. I suspect, if the connection to my database was slow, and/or, the number of people running the same process were substantial, I would have errors, such as duplicate PKs.



        My answer to that would be to assign the datarow field values to variables, run the refresh, then assign the variables to the fields and save immediately.



        Perhaps another way would be to get the new PK, then save an empty record, and then fill the record, except that enough of the fields in my table are REQUIRED so I might as well not try creating a blank record first.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Based on the feedback here and elsewhere, the following code worked for me:



              TestDataSet.GetChanges()
          testTableAdapter.Fill(TestDataSet.log_test)
          log_testDataGridView.Refresh()


          What I needed to do was, create a new row, and go get the next value for the Primary Key(VARCHAR, NOT INT, because revisions got an "R" appended to the PK...not my rule...the rule of the company).



          I wanted to put the refresh as close to the getting of the PK, which I had last after assigning values to the new datarow so it would get the latest Max +1.



          So, I put the above code just before looking up the PK, and after assigning values to the datarow, other than PK. The code above caused the datarow to blank out. So, I put the above code just prior to the creation of the new DataRow.



          For my code, this caused the code to get the latest data from the SQL table, then add the new datarow, and finally determine the last PK. Because the data used to populate the datarow is off my form, and there is no caclulations, the code runs fast enough for my needs. I suspect, if the connection to my database was slow, and/or, the number of people running the same process were substantial, I would have errors, such as duplicate PKs.



          My answer to that would be to assign the datarow field values to variables, run the refresh, then assign the variables to the fields and save immediately.



          Perhaps another way would be to get the new PK, then save an empty record, and then fill the record, except that enough of the fields in my table are REQUIRED so I might as well not try creating a blank record first.






          share|improve this answer












          Based on the feedback here and elsewhere, the following code worked for me:



              TestDataSet.GetChanges()
          testTableAdapter.Fill(TestDataSet.log_test)
          log_testDataGridView.Refresh()


          What I needed to do was, create a new row, and go get the next value for the Primary Key(VARCHAR, NOT INT, because revisions got an "R" appended to the PK...not my rule...the rule of the company).



          I wanted to put the refresh as close to the getting of the PK, which I had last after assigning values to the new datarow so it would get the latest Max +1.



          So, I put the above code just before looking up the PK, and after assigning values to the datarow, other than PK. The code above caused the datarow to blank out. So, I put the above code just prior to the creation of the new DataRow.



          For my code, this caused the code to get the latest data from the SQL table, then add the new datarow, and finally determine the last PK. Because the data used to populate the datarow is off my form, and there is no caclulations, the code runs fast enough for my needs. I suspect, if the connection to my database was slow, and/or, the number of people running the same process were substantial, I would have errors, such as duplicate PKs.



          My answer to that would be to assign the datarow field values to variables, run the refresh, then assign the variables to the fields and save immediately.



          Perhaps another way would be to get the new PK, then save an empty record, and then fill the record, except that enough of the fields in my table are REQUIRED so I might as well not try creating a blank record first.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 26 '14 at 19:58









          ANonymousest

          1




          1






















              up vote
              0
              down vote













              Imports System.Data.SqlClient
              Public Class Form4
              Dim con As New SqlConnection
              Dim cmd As New SqlCommand
              Dim str As String
              Dim count As Integer

              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
              con.Open()
              ‘ cmd = New SqlCommand("select * from Doctor", con)
              str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
              cmd = New SqlCommand(str, con)
              count = cmd.ExecuteNonQuery()
              MessageBox.Show(count & " Record inserted")

              con.close()
              End Sub

              Imports System.Data.SqlClient
              Public Class Form4
              Dim con As New SqlConnection
              Dim cmd As New SqlCommand
              Dim str As String
              Dim count As Integer
              Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
              con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
              con.Open()
              cmd = New SqlCommand("select * from Patient", con)
              cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
              cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
              cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
              cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
              ‘you can take any row in your program
              count = cmd.ExecuteNonQuery()
              MessageBox.Show("Record Deleted")
              End Sub





              share|improve this answer



























                up vote
                0
                down vote













                Imports System.Data.SqlClient
                Public Class Form4
                Dim con As New SqlConnection
                Dim cmd As New SqlCommand
                Dim str As String
                Dim count As Integer

                Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                con.Open()
                ‘ cmd = New SqlCommand("select * from Doctor", con)
                str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
                cmd = New SqlCommand(str, con)
                count = cmd.ExecuteNonQuery()
                MessageBox.Show(count & " Record inserted")

                con.close()
                End Sub

                Imports System.Data.SqlClient
                Public Class Form4
                Dim con As New SqlConnection
                Dim cmd As New SqlCommand
                Dim str As String
                Dim count As Integer
                Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                con.Open()
                cmd = New SqlCommand("select * from Patient", con)
                cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
                cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
                cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
                cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
                ‘you can take any row in your program
                count = cmd.ExecuteNonQuery()
                MessageBox.Show("Record Deleted")
                End Sub





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Imports System.Data.SqlClient
                  Public Class Form4
                  Dim con As New SqlConnection
                  Dim cmd As New SqlCommand
                  Dim str As String
                  Dim count As Integer

                  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                  con.Open()
                  ‘ cmd = New SqlCommand("select * from Doctor", con)
                  str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
                  cmd = New SqlCommand(str, con)
                  count = cmd.ExecuteNonQuery()
                  MessageBox.Show(count & " Record inserted")

                  con.close()
                  End Sub

                  Imports System.Data.SqlClient
                  Public Class Form4
                  Dim con As New SqlConnection
                  Dim cmd As New SqlCommand
                  Dim str As String
                  Dim count As Integer
                  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                  con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                  con.Open()
                  cmd = New SqlCommand("select * from Patient", con)
                  cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
                  ‘you can take any row in your program
                  count = cmd.ExecuteNonQuery()
                  MessageBox.Show("Record Deleted")
                  End Sub





                  share|improve this answer














                  Imports System.Data.SqlClient
                  Public Class Form4
                  Dim con As New SqlConnection
                  Dim cmd As New SqlCommand
                  Dim str As String
                  Dim count As Integer

                  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                  con.Open()
                  ‘ cmd = New SqlCommand("select * from Doctor", con)
                  str = "insert into Doctor values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "' )"
                  cmd = New SqlCommand(str, con)
                  count = cmd.ExecuteNonQuery()
                  MessageBox.Show(count & " Record inserted")

                  con.close()
                  End Sub

                  Imports System.Data.SqlClient
                  Public Class Form4
                  Dim con As New SqlConnection
                  Dim cmd As New SqlCommand
                  Dim str As String
                  Dim count As Integer
                  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                  con = New SqlConnection("server=SHREE-PC;database=Hospital;INTEGRATED SECURITY=SSPI;")
                  con.Open()
                  cmd = New SqlCommand("select * from Patient", con)
                  cmd = New SqlCommand("Delete from Patient where Name ='" & TextBox1.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Address='" & TextBox2.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Dieses='" & TextBox3.Text & "'", con)
                  cmd = New SqlCommand("Delete from Patient where Patient_no=" & TextBox4.Text & "", con)
                  ‘you can take any row in your program
                  count = cmd.ExecuteNonQuery()
                  MessageBox.Show("Record Deleted")
                  End Sub






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 12 '14 at 4:38









                  Shell

                  5,34752457




                  5,34752457










                  answered Apr 12 '14 at 4:14









                  user3525900

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14806190%2finsert-a-record-in-sql-database-using-vb-net-datatable-and-datarow-features%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma