.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Error message

Posted By: srini     Posted Date: February 05, 2011    Points:2   Category :VB.Net
Hi,

While running the application, I am getting a error message like "Object reference not set to an instance of an object".

I have used dataset and dataadapter to fill up the data from table. So, I set "new" keyword to create an instance for Dataset and Dataadapter.

Still I am getting this error. Please help me.

Thanks,
Srini.


Responses
Author: shankey             
Posted Date: February 05, 2011     Points: 5   

hi,

Please post your code it will help us to dig it out...

Thanks,
Shankey

Shankey
Author: srini             
Posted Date: February 05, 2011     Points: 5   

This is my code:

Imports System.Data.SqlClient
Public Class User_Edit
Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
myCommand = New SqlCommand("update Tbl_UserDetails set Usr_Fname='" + txtFname.Text + "',Usr_Lname='" + txtLname.Text + "',Usr_Gender='" + txtGender.Text + "',Usr_DOB='" + txtDOB.Text + "',Usr_Address1='" + txtAddr1.Text + "',Usr_Address2='" + txtAddr2.Text + "',Usr_City='" + txtCity.Text + "',Usr_State='" + txtState.Text + "',Usr_Country='" + txtCountry.Text + "',Usr_Zip='" + txtZip.Text + "',Usr_Email='" + txtEmail.Text + "',Usr_Phone='" + txtPhone.Text + "',Usr_Mobile='" + txtMobile.Text + "' where Usr_Fname='" + txtFname.Text + "' ", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
myConnection = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
myCommand = New SqlCommand("update Tbl_User set Usr_Type='" + txtUsertype.Text + "',Usr_Pwd='" + txtPass.Text + "' where Usr_Name= '" + txtUsername.Text + "' ", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Updated")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Private Sub User_Edit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim conn As SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
conn = New SqlConnection("server=.\SQLExpress;Initial Catalog=BillMasterDB;Integrated Security=True")
conn.Open()
da = New SqlDataAdapter("select Tbl_User.Usr_Name,Tbl_User.Usr_Type,Tbl_User.Usr_Pwd,Tbl_UserDetails.Usr_Fname,Tbl_UserDetails.Usr_Lname,Tbl_UserDetails.Usr_Gender,Tbl_UserDetails.Usr_DOB,Tbl_UserDetails.Usr_Address1,Tbl_UserDetails.Usr_Address2,Tbl_UserDetails.Usr_City,Tbl_UserDetails.Usr_State,Tbl_UserDetails.Usr_Country,Tbl_UserDetails.Usr_Zip,Tbl_UserDetails.Usr_Email,Tbl_UserDetails.Usr_Phone,Tbl_UserDetails.Usr_Mobile from Tbl_User inner join Tbl_UserDetails on Tbl_User.Usr_Name=Tbl_UserDetails.Usr_Fname", conn)

ds = New DataSet("UserEdit")
da.Fill(ds, "UserEdit")
DataGridView1.DataSource = ds.Tables("UserEdit")
conn.Close()


txtUsername.Text = User_Management.DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtUsertype.Text = User_Management.DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtPass.Text = User_Management.DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtFname.Text = User_Management.DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtLname.Text = User_Management.DataGridView1.CurrentRow.Cells(4).Value.ToString()
txtGender.Text = User_Management.DataGridView1.CurrentRow.Cells(5).Value.ToString()
txtDOB.Text = User_Management.DataGridView1.CurrentRow.Cells(6).Value.ToString()
txtAddr1.Text = User_Management.DataGridView1.CurrentRow.Cells(7).Value.ToString()
txtAddr2.Text = User_Management.DataGridView1.CurrentRow.Cells(8).Value.ToString()
txtCity.Text = User_Management.DataGridView1.CurrentRow.Cells(9).Value.ToString()
txtState.Text = User_Management.DataGridView1.CurrentRow.Cells(10).Value.ToString()
txtCountry.Text = User_Management.DataGridView1.CurrentRow.Cells(11).Value.ToString()
txtZip.Text = User_Management.DataGridView1.CurrentRow.Cells(12).Value.ToString()
txtEmail.Text = User_Management.DataGridView1.CurrentRow.Cells(13).Value.ToString()
txtPhone.Text = User_Management.DataGridView1.CurrentRow.Cells(14).Value.ToString()
txtMobile.Text = User_Management.DataGridView1.CurrentRow.Cells(15).Value.ToString()


End Sub
End Class

Srini
Author: shankey             
Posted Date: February 05, 2011     Points: 5   

hi,

following code works for me, I combined both tabel just to save time and it load and update data well

Imports System.Data.SqlClient
Public Class Form1

Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
conn = New SqlConnection("Data Source=zzz\SQLEXPRESS;Initial Catalog=db1;Persist Security Info=True;User ID=xxx;Password=test")
conn.Open()
da = New SqlDataAdapter("select Usr_Name,Usr_Type,Usr_Pwd,Usr_Fname,Usr_Lname,Usr_Gender,Usr_DOB,Usr_Address1,Usr_Address2,Usr_City,Usr_State,Usr_Country,Usr_Zip,Usr_Email,Usr_Phone,Usr_Mobile from Tbl_User ", conn)

ds = New DataSet("UserEdit")
da.Fill(ds, "UserEdit")
DataGridView1.DataSource = ds.Tables("UserEdit")
conn.Close()


txtUsername.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
txtUsertype.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
txtPass.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
txtFname.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
txtLname.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString()
txtGender.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString()
txtDOB.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString()
txtAddr1.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString()
txtAddr2.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString()
txtCity.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString()
txtState.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString()
txtCountry.Text = DataGridView1.CurrentRow.Cells(11).Value.ToString()
txtZip.Text = DataGridView1.CurrentRow.Cells(12).Value.ToString()
txtEmail.Text = DataGridView1.CurrentRow.Cells(13).Value.ToString()
txtPhone.Text = DataGridView1.CurrentRow.Cells(14).Value.ToString()
txtMobile.Text = DataGridView1.CurrentRow.Cells(15).Value.ToString()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myConnection = New SqlConnection("Data Source=zzz\SQLEXPRESS;Initial Catalog=db1;Persist Security Info=True;User ID=xxx;Password=test")
myCommand = New SqlCommand("update Tbl_User set Usr_Type='" + txtUsertype.Text + "',Usr_Pwd='" + txtPass.Text + "' where Usr_Name= '" + txtUsername.Text + "' ", myConnection)
Try
myConnection.Open()
ra = myCommand.ExecuteNonQuery()
MessageBox.Show("Updated")
myConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class


Sample solution is attached for you reference, but do change following line in attached code
myConnection = New SqlConnection("Data Source=zzz\SQLEXPRESS;Initial Catalog=db1;Persist Security Info=True;User ID=xxx;Password=test")

Data Source is server name

Initial Catalog is database name
User ID is user name/id and password is your passowd

Download Attachment

Shankey
Author: srini             
Posted Date: February 05, 2011     Points: 5   

Hi,

Thanks for your help. By the way,your source file is not opening on my machine.

Your code is not working.

If i removed User_Management from
txtUsername.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()

its showing first record instead of second third and so on.


Help me!



Srini
Author: srini             
Posted Date: February 05, 2011     Points: 5   

Hi,

Please help me soon...

Thanks,
Srini.

Srini


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend