Click here to Skip to main content
15,742,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I want to convert the below code into c# .net.But the error occurs.plz rectify it.
In the below code i can't get converted to c#.Below line shows error
"If Page.IsPostBack = False Then
BindGrid()"
and also "Row" and "Gridview1.datasource" also showing error.
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim oDs As New DataSet
        oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        GridView1.DataSource = oDs
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        BindGrid()
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource
        oDs.Tables(0).Rows(GridView1.Rows(e.RowIndex).DataItemIndex).Delete()
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        BindGrid()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
        Dim strId As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        Dim strName As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        Dim strTel As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text

        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource

        oDs.Tables(0).Rows(i).Item(0) = strId
        oDs.Tables(0).Rows(i).Item(1) = strName
        oDs.Tables(0).Rows(i).Item(2) = strTel

        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource
        Dim oDr As DataRow = oDs.Tables(0).NewRow
        oDr("id") = txtId.Text
        oDr("name") = txtName.Text
        oDr("phone") = txtTel.Text
        oDs.Tables(0).Rows.Add(oDr)
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()
    End Sub
End Class
Posted
Comments
Sandeep Mewara 14-Mar-11 6:33am    
What error?
sridharan28 14-Mar-11 6:40am    
"Cannot implicitly convert type 'object' to 'System.Data.DataSet'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\sridharan\Desktop\sridharan\027_edit_xml_src\Default3.aspx.cs 63 23 C:\...\027_edit_xml_src\"

Try out below code. It should work.
Only 2 issues in code converted from above link provided by OriginalGriff

1. GridView row/cell accessing in code behind should be with square brackets(i.e. "[". "]")
2. GridView source should be parse to DataSet. [ I am not sure if it will work or not :( ]

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.UI.WebControls;
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    public void BindGrid()
    {
        DataSet oDs = new DataSet();
        oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        GridView1.DataSource = oDs;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
    protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        oDs.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
    protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        int i = GridView1.Rows[e.RowIndex].DataItemIndex;
        string strId = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string strTel = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        GridView1.EditIndex = -1;
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        oDs.Tables[0].Rows[i][0] = strId;
        oDs.Tables[0].Rows[i][1] = strName;
        oDs.Tables[0].Rows[i][2] = strTel;
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        DataRow oDr = oDs.Tables[0].NewRow();
        oDr["id"] = txtId.Text;
        oDr["name"] = txtName.Text;
        oDr["phone"] = txtTel.Text;
        oDs.Tables[0].Rows.Add(oDr);
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
   
}


Hope it helps.
 
Share this answer
 
Comments
sridharan28 14-Mar-11 6:50am    
hey.Thanks.Now it's working
That's Aragon 14-Mar-11 7:23am    
You are welcome. Glad to know that it works for you. :)
I'm not sure what your problem is, I can't see anything massively wrong with the code.
There is an on-line converter between VB and C# here: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] - I use it quite a bit!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900