473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Hi! 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 show me an error.(Object reference not set to an instance of an object).Plz help me!


Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2.  
  3. Public Class Form1
  4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5. Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=drpractice;Integrated Security=True")
  6. Dim da As New SqlDataAdapter
  7. Dim ds As New DataSet
  8. Try
  9. cn.Open()
  10. da.SelectCommand = New SqlCommand("SELECT * FROM [emp_tbl]", cn)
  11. da.Fill(ds)
  12. Dim dt As New DataTable
  13. dt = ds.Tables("emp_tbl")
  14. Dim dr As DataRow = dt.NewRow()
  15. dr.Item("emp_id") = TextBox1.Text.Trim
  16. dr.Item("emp_name") = TextBox2.Text.Trim
  17. dr.Item("salary") = TextBox3.Text.Trim
  18. dr.Item("age") = TextBox4.Text.Trim
  19. dr.Item("emp_group") = TextBox5.Text.Trim
  20. dt.Rows.Add(dr)
  21. da.Update(ds)
  22. MsgBox("Record Successfully Inserted")
  23. Catch ex As Exception
  24. MsgBox(ex.Message)
  25. End Try
  26. End Sub
  27. End Class 
Feb 11 '13 #1
1 4869
zmbd
5,501 Expert Mod 4TB
By, simply stating that your code "doesn't work," posting a generic/jargon filled error along with code that appears to have had very little if any troubleshooting performed, and expecting someone to help doesn't usually result in much of an answer and may result in your thread being deleted. Instead, please tell us what you were expecting to happen, what actually happened, for each error: the EXACT title, error number, and descriptions that occurred and at what line in your posted code the error occurred. These are the minimum requirements for posting a question of this nature.

Bytes is not a code nor homework service. Please read the FAQ and posting guidelines before re-posting your question.
Before Posting (VBA or SQL) Code.
How to ask "good" questions -- READ BEFORE SUBMITTING A QUESTION!.
POSTING_GUIDELINES: Please Read Carefully Before Posting to a Forum.
Feb 11 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Deepankar | last post by:
Hi, I was trying to change an example for SQL Server to work with Access db to insert image data. I have everything working except getting the OleDbParameter type for the image column. The...
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
7
by: Wayne Gibson | last post by:
Hi All, Was wondering if anybody could help. I'm currently trying to copy a record from datatable to another. I have two problems: 1) Transferring the record from one datatable to another. 2)...
3
by: Indrachapa | last post by:
I want to get how to add data to table in SQL server database using a datatable using ASP.net
1
by: joel | last post by:
hi guys... i am a student, i am studying and i am a newbie in VB.net and i am using SQL Server 2000. I have this datatable which i insert rows (may take up to 2 or more rows because i would...
1
by: gomathinayagam | last post by:
hai, am only beginer in c#... i am trying to connect database with webform. using a technique that the fields of the table and the controls in a form are named same...then i try get the controls...
5
by: eihabisaac | last post by:
hi i have a field in my database (status) which have the value 0 or 1 how can i update these value using check box if checked then 1 if not checked then 0 and also how to insert values...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
1
by: dipalichavan82 | last post by:
i m using asp.net vs2005.i have gridview on page.it displays datatable values.i have insert button on page.i want to insert data in textbox into database.i m using disconnected archi. protected...
4
by: MissElegant | last post by:
Hi all, I have tried to do a test to a lesson which was in the internet, but it doesn't work? ANYBody here to help please?? The problem that what I enter in the textbox should be sent to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.