Sir,
When i am executing the below code on Visual Studio 2008, then it is executing properly with out any error. But when i am executing it on Visual Studio 2012, then it showing me following error (System.NullReferenceException: Object reference not set to an instance of an object) at line 64
Why is it so?
.vb Code is below :
Imports System.Data.SqlClient
Imports System.Data
Partial Class grid
Inherits System.Web.UI.Page
Function constr() As String
Dim i As New IO.StreamReader(Server.MapPath("constr.config"))
Dim str As String = i.ReadToEnd
i.Close()
Return str
End Function
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
filldata()
End If
End Sub
Sub filldata()
Try
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand("select * from clienttab order by id desc", con)
Dim da As New SqlDataAdapter
Dim ds As New DataSet
da.SelectCommand = cmd
da.Fill(ds, "t")
GridView1.DataSource = ds.Tables(0).DefaultView
GridView1.DataBind()
con.Close()
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
filldata()
End Sub
Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Try
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand("delete from clienttab where id=" & GridView1.Rows(e.RowIndex).Cells(0).Text.ToString, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
GridView1.EditIndex = -1
Response.Write("Data deleted")
filldata()
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
filldata()
End Sub
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Try
Dim gr As GridViewRow
gr = GridView1.Rows(e.RowIndex)
Dim name1 As String = CType(gr.FindControl("lname"), Label).Text
Dim contact1 As String = CType(gr.FindControl("lcontact"), Label).Text
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand("update clienttab set name1=@a,contact1=@b where id=" & GridView1.Rows(e.RowIndex).Cells(0).Text.ToString, con)
cmd.Parameters.AddWithValue("@a", name1)
cmd.Parameters.AddWithValue("@b", contact1)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
GridView1.EditIndex = -1
Response.Write("Data updated!")
filldata()
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Private Function TextBox() As Object
Throw New NotImplementedException
End Function
End Class
html code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" EmptyDataText="no record found" ForeColor="#333333" GridLines="None" AllowPaging="True" HorizontalAlign="Center" Width="303px" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" HorizontalAlign="Center" VerticalAlign="Middle" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:BoundField DataField="ID" ReadOnly="true" HeaderText="ID" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lname" Text='<%# Bind("name1") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tname" runat="server" Text='<%# Bind("name1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact">
<ItemTemplate>
<asp:Label ID="lcontact" Text='<%# Bind("contact1") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tcon" runat="server" Text='<%# Bind("contact1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>