Need Help... Convert VB.NET to C#

M

manmit.walia

Hello All,
I am stuck on a conversion problem. I am trying to convert my
application which is written in VB.NET to C# because the project I am
working on currently is being written in C#.
I tried my best to convert it, but somehow the app does not work. I am
also not getting any errors when I complie thus, letting me know I am
on the write track. Basically what I want to do is edit,add,delete, and
update an XML file in a DataGrid. Below is my Code in

sub LoadXML
Dim objdata as New DataSet
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
datagrid1.Datasource=objdata
datagrid1.Databind()

Catch

CreateXML

End Try
End sub


Sub CreateXML
Dim objdata as New DataSet("IndianMaster")
Dim dt as New DataTable("Product")
Dim dr as DataRow
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

dt.Columns.Add(New DataColumn("NAME"))
dt.Columns.Add(New DataColumn("SKU"))
dt.Columns.Add(New DataColumn("PRICE"))
dt.Columns.Add(New DataColumn("TYPE"))

dr=dt.NewRow()
dr(0)="No Data"
dr(1)="No Data"
dr(2)="No Data"
dr(3)="No Data"

dt.Rows.Add(dr)

objdata.Tables.Add(dt)

datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

LoadXML()
End Sub


Sub setEditMode(Sender as Object, E as DataGridCommandEventArgs)

Dim objdata as New DataSet
Dim x1 as string
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error4.visible="False"
datagrid1.EditItemIndex=E.Item.ItemIndex
datagrid1.showfooter="false"
LoadXML
Else
error4.visible="True"
End If
End sub


Sub cancelEdit(sender as Object, E as DataGridCommandEventArgs)
datagrid1.EditItemIndex=-1
datagrid1.showfooter="true"
error1.visible="False"
error2.visible="False"
error3.visible="False"
error4.visible="False"
error5.visible="False"

LoadXML

End sub


Sub DelXML(S as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Delete" Then
If datagrid1.EditItemIndex=-1 Then
error5.visible="False"
Dim x1 as string

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

Dim objdata as New DataSet

Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
x1 & "'"
If objdata.Tables("Product").DefaultView.Count>0 Then
objdata.Tables("Product").DefaultView.Delete(0)
End If

objdata.Tables("Product").DefaultView.RowFilter=""


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
Catch
CreateXML
End Try

LoadXML
Else
error5.visible="True"
End If
End if
End Sub


Sub UpdateXML(s as Object, e as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Update" Then
Dim str1, str2, str3,v1 as string
Dim txt1,txt2,txt3,txt4 as Textbox
Dim objdata as New DataSet
Dim x1 as string



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

v1=e.Item.ItemIndex

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error3.visible="False"

txt1=e.Item.FindControl("name_edit")
txt2=e.Item.FindControl("sku_edit")
txt3=e.Item.FindControl("price_edit")
txt4=e.Item.FindControl("type_edit")


objdata.Tables("Product").Rows(v1).Item("NAME")=txt1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=txt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=txt4.Text
datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

datagrid1.EditItemIndex=-1
LoadXML
datagrid1.showfooter="true"
Else
error3.visible="True"
End If
End If
End sub


Sub doInsert(s as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

if e.CommandName="doAdd" Then
Dim v1 as string
Dim tadd1,tadd2,tadd3,tadd4 as Textbox
Dim objdata as New DataSet
Dim dr as DataRow



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

tadd1=e.Item.FindControl("name_add")
tadd2=e.Item.FindControl("sku_add")
tadd3=e.Item.FindControl("price_add")
tadd4=e.Item.FindControl("type_add")

Try
'tadd1.text=int32.parse(tadd1.text)
error2.visible="false"

If tadd1.text="" Then
' tadd1.Text=""
error2.visible="true"
End If
Catch
tadd1.text=""
error2.visible="true"
End Try

If tadd1.Text<>"" and tadd2.Text<>"" and tadd3.Text<>"" Then
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
tadd1.Text & "'"
If objdata.Tables("Product").DefaultView.Count<=0 Then
error1.visible="False"
objdata.Tables("Product").DefaultView.RowFilter=""
dr=objdata.Tables("Product").NewRow()

dr(0)=tadd1.Text
dr(1)=tadd2.Text
dr(2)=tadd3.Text
dr(3)=tadd4.Text
objdata.Tables("Product").Rows.Add(dr)


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
LoadXML
Else
error1.visible="True"
error1.Text="Product must be unique"
End If
End If
End If
End Sub

Here is my C# Code:

void LoadXML()
{
DataSet objdata = new DataSet();
orderc = user.SelectedItem.Value.ToString();
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc + ".xml"));
datagrid1.DataSource = objdata;
datagrid1.DataBind();

}

private DataSet CreateDataSource()
{
XmlDataDocument mydata = new XmlDataDocument();
orderc = user.SelectedItem.Value.ToString();
mydata.DataSet.ReadXml(Server.MapPath("OrderGuides/" + orderc +
".xml"));
return mydata.DataSet;
}


void CreateXML()
{
DataSet objdata = new DataSet("IndianMaster");
DataTable dt = new DataTable("Product");
DataRow dr;
orderc = user.SelectedItem.Value.ToString();
dt.Columns.Add("NAME");
dt.Columns.Add("SKU");
dt.Columns.Add("PRICE");
dt.Columns.Add("TYPE");
dr = dt.NewRow();
dr[0] = "No Data";
dr[1] = "No Data";
dr[2] = "No Data";
dr[3] = "No Data";
dt.Rows.Add(dr);
objdata.Tables.Add(dt);
datagrid1.DataSource=objdata;
datagrid1.DataBind();
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));
LoadXML();

}

public void doEdit (Object sender, DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex = e.Item.ItemIndex;
LoadXML();
}


public void doCancel(Object sender, DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex = -1;
LoadXML();
}


public void DelXML (object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
if(e.CommandName == "Delete")
{
if(datagrid1.EditItemIndex == -1)
{
string x1;
x1 = datagrid1.DataKeys[e.Item.ItemIndex].ToString();
DataSet objdata = new DataSet();
try
{
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc +
".xml"));
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + x1 +
"'";
if(objdata.Tables["Product"].DefaultView.Count > 0)
{
objdata.Tables["Product"].DefaultView.Delete(0);
}
objdata.Tables["Product"].DefaultView.RowFilter = "";
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));

}

catch
{
}
finally
{
CreateXML();
}
LoadXML();


}

}
}


public void doUpdate(Object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
string N = ((TextBox)e.Item.Cells[0].Controls[1]).Text;
string S = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string P = ((TextBox)e.Item.Cells[2].Controls[1]).Text;
string T = ((TextBox)e.Item.Cells[3].Controls[1]).Text;

datagrid1.EditItemIndex = -1;
DataSet ds = CreateDataSource();
DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];

row["NAME"]=N;
row["SKU"]=S;
row["PRICE"]=P;
row["TYPE"]=T;

ds.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));

//mygrid.DataSource = CreateDataSource();
LoadXML();
}



public void doInsert (object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
if(e.CommandName == "doAdd")
{
//string v1;
TextBox tadd1,tadd2,tadd3,tadd4 = new TextBox();
DataSet objdata = new DataSet();
DataRow dr;
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc + ".xml"));
tadd1 = (TextBox)e.Item.FindControl("name_add");
tadd2 = (TextBox)e.Item.FindControl("sku_add");
tadd3 = (TextBox)e.Item.FindControl("price_add");
tadd4 = (TextBox)e.Item.FindControl("type_add");

if(tadd1.Text != "" && tadd2.Text != "" && tadd3.Text != "" )
{
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" +
tadd1.Text + "'";
if(objdata.Tables["Product"].DefaultView.Count <= 0)
{
objdata.Tables["Product"].DefaultView.RowFilter = "";
dr = objdata.Tables["Product"].NewRow();
dr[0] = tadd1.Text;
dr[1] = tadd2.Text;
dr[2] = tadd3.Text;
dr[3] = tadd4.Text;
objdata.Tables["Product"].Rows.Add(dr);
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));
LoadXML();

}
}


}
}
 
M

Mark Nijhof

You could create a new c# project in VS and than copy the code into the new
files (.cs) than the compiler would complain if it doesn't understand
something. The extention tells it it should contain c# code and not VB.

Also you could just put the functions that do something into a class and use
the VB class in your c# projects (include the dll in references) but than
make sure only to include the functions that do not directly interact with
the GUI, you could pass references to the function, but that I would not do,
instead pass it a dataset.

-Mark


Hello All,
I am stuck on a conversion problem. I am trying to convert my
application which is written in VB.NET to C# because the project I am
working on currently is being written in C#.
I tried my best to convert it, but somehow the app does not work. I am
also not getting any errors when I complie thus, letting me know I am
on the write track. Basically what I want to do is edit,add,delete, and
update an XML file in a DataGrid. Below is my Code in

sub LoadXML
Dim objdata as New DataSet
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
datagrid1.Datasource=objdata
datagrid1.Databind()

Catch

CreateXML

End Try
End sub


Sub CreateXML
Dim objdata as New DataSet("IndianMaster")
Dim dt as New DataTable("Product")
Dim dr as DataRow
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

dt.Columns.Add(New DataColumn("NAME"))
dt.Columns.Add(New DataColumn("SKU"))
dt.Columns.Add(New DataColumn("PRICE"))
dt.Columns.Add(New DataColumn("TYPE"))

dr=dt.NewRow()
dr(0)="No Data"
dr(1)="No Data"
dr(2)="No Data"
dr(3)="No Data"

dt.Rows.Add(dr)

objdata.Tables.Add(dt)

datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

LoadXML()
End Sub


Sub setEditMode(Sender as Object, E as DataGridCommandEventArgs)

Dim objdata as New DataSet
Dim x1 as string
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error4.visible="False"
datagrid1.EditItemIndex=E.Item.ItemIndex
datagrid1.showfooter="false"
LoadXML
Else
error4.visible="True"
End If
End sub


Sub cancelEdit(sender as Object, E as DataGridCommandEventArgs)
datagrid1.EditItemIndex=-1
datagrid1.showfooter="true"
error1.visible="False"
error2.visible="False"
error3.visible="False"
error4.visible="False"
error5.visible="False"

LoadXML

End sub


Sub DelXML(S as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Delete" Then
If datagrid1.EditItemIndex=-1 Then
error5.visible="False"
Dim x1 as string

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

Dim objdata as New DataSet

Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
x1 & "'"
If objdata.Tables("Product").DefaultView.Count>0 Then
objdata.Tables("Product").DefaultView.Delete(0)
End If

objdata.Tables("Product").DefaultView.RowFilter=""


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
Catch
CreateXML
End Try

LoadXML
Else
error5.visible="True"
End If
End if
End Sub


Sub UpdateXML(s as Object, e as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Update" Then
Dim str1, str2, str3,v1 as string
Dim txt1,txt2,txt3,txt4 as Textbox
Dim objdata as New DataSet
Dim x1 as string



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

v1=e.Item.ItemIndex

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error3.visible="False"

txt1=e.Item.FindControl("name_edit")
txt2=e.Item.FindControl("sku_edit")
txt3=e.Item.FindControl("price_edit")
txt4=e.Item.FindControl("type_edit")


objdata.Tables("Product").Rows(v1).Item("NAME")=txt1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=txt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=txt4.Text
datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

datagrid1.EditItemIndex=-1
LoadXML
datagrid1.showfooter="true"
Else
error3.visible="True"
End If
End If
End sub


Sub doInsert(s as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

if e.CommandName="doAdd" Then
Dim v1 as string
Dim tadd1,tadd2,tadd3,tadd4 as Textbox
Dim objdata as New DataSet
Dim dr as DataRow



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

tadd1=e.Item.FindControl("name_add")
tadd2=e.Item.FindControl("sku_add")
tadd3=e.Item.FindControl("price_add")
tadd4=e.Item.FindControl("type_add")

Try
'tadd1.text=int32.parse(tadd1.text)
error2.visible="false"

If tadd1.text="" Then
' tadd1.Text=""
error2.visible="true"
End If
Catch
tadd1.text=""
error2.visible="true"
End Try

If tadd1.Text<>"" and tadd2.Text<>"" and tadd3.Text<>"" Then
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
tadd1.Text & "'"
If objdata.Tables("Product").DefaultView.Count<=0 Then
error1.visible="False"
objdata.Tables("Product").DefaultView.RowFilter=""
dr=objdata.Tables("Product").NewRow()

dr(0)=tadd1.Text
dr(1)=tadd2.Text
dr(2)=tadd3.Text
dr(3)=tadd4.Text
objdata.Tables("Product").Rows.Add(dr)


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
LoadXML
Else
error1.visible="True"
error1.Text="Product must be unique"
End If
End If
End If
End Sub

Here is my C# Code:

void LoadXML()
{
DataSet objdata = new DataSet();
orderc = user.SelectedItem.Value.ToString();
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc + ".xml"));
datagrid1.DataSource = objdata;
datagrid1.DataBind();

}

private DataSet CreateDataSource()
{
XmlDataDocument mydata = new XmlDataDocument();
orderc = user.SelectedItem.Value.ToString();
mydata.DataSet.ReadXml(Server.MapPath("OrderGuides/" + orderc +
".xml"));
return mydata.DataSet;
}


void CreateXML()
{
DataSet objdata = new DataSet("IndianMaster");
DataTable dt = new DataTable("Product");
DataRow dr;
orderc = user.SelectedItem.Value.ToString();
dt.Columns.Add("NAME");
dt.Columns.Add("SKU");
dt.Columns.Add("PRICE");
dt.Columns.Add("TYPE");
dr = dt.NewRow();
dr[0] = "No Data";
dr[1] = "No Data";
dr[2] = "No Data";
dr[3] = "No Data";
dt.Rows.Add(dr);
objdata.Tables.Add(dt);
datagrid1.DataSource=objdata;
datagrid1.DataBind();
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));
LoadXML();

}

public void doEdit (Object sender, DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex = e.Item.ItemIndex;
LoadXML();
}


public void doCancel(Object sender, DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex = -1;
LoadXML();
}


public void DelXML (object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
if(e.CommandName == "Delete")
{
if(datagrid1.EditItemIndex == -1)
{
string x1;
x1 = datagrid1.DataKeys[e.Item.ItemIndex].ToString();
DataSet objdata = new DataSet();
try
{
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc +
".xml"));
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + x1 +
"'";
if(objdata.Tables["Product"].DefaultView.Count > 0)
{
objdata.Tables["Product"].DefaultView.Delete(0);
}
objdata.Tables["Product"].DefaultView.RowFilter = "";
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));

}

catch
{
}
finally
{
CreateXML();
}
LoadXML();


}

}
}


public void doUpdate(Object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
string N = ((TextBox)e.Item.Cells[0].Controls[1]).Text;
string S = ((TextBox)e.Item.Cells[1].Controls[1]).Text;
string P = ((TextBox)e.Item.Cells[2].Controls[1]).Text;
string T = ((TextBox)e.Item.Cells[3].Controls[1]).Text;

datagrid1.EditItemIndex = -1;
DataSet ds = CreateDataSource();
DataRow row = ds.Tables[0].Rows[e.Item.ItemIndex];

row["NAME"]=N;
row["SKU"]=S;
row["PRICE"]=P;
row["TYPE"]=T;

ds.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));

//mygrid.DataSource = CreateDataSource();
LoadXML();
}



public void doInsert (object sender, DataGridCommandEventArgs e)
{
orderc = user.SelectedItem.Value.ToString();
if(e.CommandName == "doAdd")
{
//string v1;
TextBox tadd1,tadd2,tadd3,tadd4 = new TextBox();
DataSet objdata = new DataSet();
DataRow dr;
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc + ".xml"));
tadd1 = (TextBox)e.Item.FindControl("name_add");
tadd2 = (TextBox)e.Item.FindControl("sku_add");
tadd3 = (TextBox)e.Item.FindControl("price_add");
tadd4 = (TextBox)e.Item.FindControl("type_add");

if(tadd1.Text != "" && tadd2.Text != "" && tadd3.Text != "" )
{
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" +
tadd1.Text + "'";
if(objdata.Tables["Product"].DefaultView.Count <= 0)
{
objdata.Tables["Product"].DefaultView.RowFilter = "";
dr = objdata.Tables["Product"].NewRow();
dr[0] = tadd1.Text;
dr[1] = tadd2.Text;
dr[2] = tadd3.Text;
dr[3] = tadd4.Text;
objdata.Tables["Product"].Rows.Add(dr);
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));
LoadXML();

}
}


}
}
 
M

manmit.walia

How accurate is this application? Also, is there a better way to do it,
instead of decompilers? I want to make sure that I understand each line
of converted code.
 
G

Guest

Here is the literal source code translation produced with our Instant C# VB
to C# converter (it's possible to produce this translation one method at a
time with our demo). One note: you were setting many boolean values to
string literals "true" and "false" - this is very unorthodox (I've never seen
this before - why not use True and False ?) and they are left as string
literals - you'll have to change that manually.

public void LoadXML()
{
DataSet objdata = new DataSet();
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;
try
{

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"));
datagrid1.DataSource=objdata;
datagrid1.DataBind();

}
catch
{

CreateXML();

}
}


public void CreateXML()
{
DataSet objdata = new DataSet("IndianMaster");
DataTable dt = new DataTable("Product");
DataRow dr = null;
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;

dt.Columns.Add(new DataColumn("NAME"));
dt.Columns.Add(new DataColumn("SKU"));
dt.Columns.Add(new DataColumn("PRICE"));
dt.Columns.Add(new DataColumn("TYPE"));

dr=dt.NewRow();
dr[0]="No Data";
dr[1]="No Data";
dr[2]="No Data";
dr[3]="No Data";

dt.Rows.Add(dr);

objdata.Tables.Add(dt);

datagrid1.DataSource=objdata;
datagrid1.DataBind();


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"));

LoadXML();
}


public void setEditMode(object Sender, DataGridCommandEventArgs E)
{

DataSet objdata = new DataSet();
string x1 = null;
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"));

x1=datagrid1.DataKeys[E.Item.ItemIndex];

objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + x1 + "'";

if (objdata.Tables["Product"].DefaultView.Count>0)
{
error4.Visible="False";
datagrid1.EditItemIndex=E.Item.ItemIndex;
datagrid1.showfooter="false";
LoadXML();
}
else
error4.Visible="True";
}


public void cancelEdit(object sender, DataGridCommandEventArgs E)
{
datagrid1.EditItemIndex=-1;
datagrid1.showfooter="true";
error1.Visible="False";
error2.Visible="False";
error3.Visible="False";
error4.Visible="False";
error5.Visible="False";

LoadXML();

}


public void DelXML(object S, DataGridCommandEventArgs E)
{
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;
if (E.CommandName=="Delete")
{
if (datagrid1.EditItemIndex==-1)
{
error5.Visible="False";
string x1 = null;

x1=datagrid1.DataKeys[E.Item.ItemIndex];

DataSet objdata = new DataSet();

try
{

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"));
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + x1 + "'";
if (objdata.Tables["Product"].DefaultView.Count>0)
objdata.Tables["Product"].DefaultView.Delete(0);

objdata.Tables["Product"].DefaultView.RowFilter="";


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"));
}
catch
{
CreateXML();
}

LoadXML();
}
else
error5.Visible="True";
}
}


public void UpdateXML(object s, DataGridCommandEventArgs e)
{
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;
if (e.CommandName=="Update")
{
string str1 = null;
string str2 = null;
string str3 = null;
string v1 = null;
TextBox txt1 = null;
TextBox txt2 = null;
TextBox txt3 = null;
TextBox txt4 = null;
DataSet objdata = new DataSet();
string x1 = null;

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"));

v1=e.Item.ItemIndex;

x1=datagrid1.DataKeys[e.Item.ItemIndex];

objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + x1 + "'";

if (objdata.Tables["Product"].DefaultView.Count>0)
{
error3.Visible="False";

txt1=e.Item.FindControl("name_edit");
txt2=e.Item.FindControl("sku_edit");
txt3=e.Item.FindControl("price_edit");
txt4=e.Item.FindControl("type_edit");


objdata.Tables["Product"].Rows[v1]["NAME"]=txt1.Text;
objdata.Tables["Product"].Rows[v1]["SKU"]=txt2.Text;
objdata.Tables["Product"].Rows[v1]["PRICE"]=txt3.Text;
objdata.Tables["Product"].Rows[v1]["TYPE"]=txt4.Text;
datagrid1.DataSource=objdata;
datagrid1.DataBind();

objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"));

datagrid1.EditItemIndex=-1;
LoadXML();
datagrid1.showfooter="true";
}
else
error3.Visible="True";
}
}


public void doInsert(object s, DataGridCommandEventArgs E)
{
string customer = null;
customer = CustomerDropDown.SelectedItem.Value.ToString();
temp.Text = customer;

if (E.CommandName=="doAdd")
{
string v1 = null;
TextBox tadd1 = null;
TextBox tadd2 = null;
TextBox tadd3 = null;
TextBox tadd4 = null;
DataSet objdata = new DataSet();
DataRow dr = null;

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"));

tadd1=E.Item.FindControl("name_add");
tadd2=E.Item.FindControl("sku_add");
tadd3=E.Item.FindControl("price_add");
tadd4=E.Item.FindControl("type_add");

try
{
//tadd1.text=int32.parse(tadd1.text)
error2.Visible="false";

if (tadd1.Text=="")
{
// tadd1.Text=""
error2.Visible="true";
}
}
catch
{
tadd1.Text="";
error2.Visible="true";
}

if (tadd1.Text!="" & tadd2.Text!="" & tadd3.Text!="")
{
objdata.Tables["Product"].DefaultView.RowFilter="NAME='" + tadd1.Text +
"'";
if (objdata.Tables["Product"].DefaultView.Count<=0)
{
error1.Visible="False";
objdata.Tables["Product"].DefaultView.RowFilter="";
dr=objdata.Tables["Product"].NewRow();

dr[0]=tadd1.Text;
dr[1]=tadd2.Text;
dr[2]=tadd3.Text;
dr[3]=tadd4.Text;
objdata.Tables["Product"].Rows.Add(dr);

objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"));
LoadXML();
}
else
{
error1.Visible="True";
error1.Text="Product must be unique";
}
}
}
}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter



Hello All,
I am stuck on a conversion problem. I am trying to convert my
application which is written in VB.NET to C# because the project I am
working on currently is being written in C#.
I tried my best to convert it, but somehow the app does not work. I am
also not getting any errors when I complie thus, letting me know I am
on the write track. Basically what I want to do is edit,add,delete, and
update an XML file in a DataGrid. Below is my Code in

sub LoadXML
Dim objdata as New DataSet
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
datagrid1.Datasource=objdata
datagrid1.Databind()

Catch

CreateXML

End Try
End sub


Sub CreateXML
Dim objdata as New DataSet("IndianMaster")
Dim dt as New DataTable("Product")
Dim dr as DataRow
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

dt.Columns.Add(New DataColumn("NAME"))
dt.Columns.Add(New DataColumn("SKU"))
dt.Columns.Add(New DataColumn("PRICE"))
dt.Columns.Add(New DataColumn("TYPE"))

dr=dt.NewRow()
dr(0)="No Data"
dr(1)="No Data"
dr(2)="No Data"
dr(3)="No Data"

dt.Rows.Add(dr)

objdata.Tables.Add(dt)

datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

LoadXML()
End Sub


Sub setEditMode(Sender as Object, E as DataGridCommandEventArgs)

Dim objdata as New DataSet
Dim x1 as string
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error4.visible="False"
datagrid1.EditItemIndex=E.Item.ItemIndex
datagrid1.showfooter="false"
LoadXML
Else
error4.visible="True"
End If
End sub


Sub cancelEdit(sender as Object, E as DataGridCommandEventArgs)
datagrid1.EditItemIndex=-1
datagrid1.showfooter="true"
error1.visible="False"
error2.visible="False"
error3.visible="False"
error4.visible="False"
error5.visible="False"

LoadXML

End sub


Sub DelXML(S as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Delete" Then
If datagrid1.EditItemIndex=-1 Then
error5.visible="False"
Dim x1 as string

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

Dim objdata as New DataSet

Try

objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
x1 & "'"
If objdata.Tables("Product").DefaultView.Count>0 Then
objdata.Tables("Product").DefaultView.Delete(0)
End If

objdata.Tables("Product").DefaultView.RowFilter=""


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
Catch
CreateXML
End Try

LoadXML
Else
error5.visible="True"
End If
End if
End Sub


Sub UpdateXML(s as Object, e as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer
If e.CommandName="Update" Then
Dim str1, str2, str3,v1 as string
Dim txt1,txt2,txt3,txt4 as Textbox
Dim objdata as New DataSet
Dim x1 as string



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

v1=e.Item.ItemIndex

x1=datagrid1.DataKeys.Item(e.Item.ItemIndex)

objdata.Tables("Product").DefaultView.RowFilter="NAME='" & x1 &
"'"

If objdata.Tables("Product").DefaultView.Count>0 Then
error3.visible="False"

txt1=e.Item.FindControl("name_edit")
txt2=e.Item.FindControl("sku_edit")
txt3=e.Item.FindControl("price_edit")
txt4=e.Item.FindControl("type_edit")


objdata.Tables("Product").Rows(v1).Item("NAME")=txt1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=txt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=txt4.Text
datagrid1.datasource=objdata
datagrid1.databind()


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))

datagrid1.EditItemIndex=-1
LoadXML
datagrid1.showfooter="true"
Else
error3.visible="True"
End If
End If
End sub


Sub doInsert(s as Object, E as DataGridCommandEventArgs)
Dim customer as String
customer = CustomerDropDown.SelectedItem.Value.ToString
temp.Text = customer

if e.CommandName="doAdd" Then
Dim v1 as string
Dim tadd1,tadd2,tadd3,tadd4 as Textbox
Dim objdata as New DataSet
Dim dr as DataRow



objdata.ReadXML(Server.Mappath("/Secured/XML/"+customer+".xml"))

tadd1=e.Item.FindControl("name_add")
tadd2=e.Item.FindControl("sku_add")
tadd3=e.Item.FindControl("price_add")
tadd4=e.Item.FindControl("type_add")

Try
'tadd1.text=int32.parse(tadd1.text)
error2.visible="false"

If tadd1.text="" Then
' tadd1.Text=""
error2.visible="true"
End If
Catch
tadd1.text=""
error2.visible="true"
End Try

If tadd1.Text<>"" and tadd2.Text<>"" and tadd3.Text<>"" Then
objdata.Tables("Product").DefaultView.RowFilter="NAME='" &
tadd1.Text & "'"
If objdata.Tables("Product").DefaultView.Count<=0 Then
error1.visible="False"
objdata.Tables("Product").DefaultView.RowFilter=""
dr=objdata.Tables("Product").NewRow()

dr(0)=tadd1.Text
dr(1)=tadd2.Text
dr(2)=tadd3.Text
dr(3)=tadd4.Text
objdata.Tables("Product").Rows.Add(dr)


objdata.WriteXML(Server.mappath("/Secured/XML/"+customer+".xml"))
LoadXML
Else
error1.visible="True"
error1.Text="Product must be unique"
End If
End If
End If
End Sub

Here is my C# Code:

void LoadXML()
{
DataSet objdata = new DataSet();
orderc = user.SelectedItem.Value.ToString();
objdata.ReadXml(Server.MapPath("OrderGuides/" + orderc + ".xml"));
datagrid1.DataSource = objdata;
datagrid1.DataBind();

}

private DataSet CreateDataSource()
{
XmlDataDocument mydata = new XmlDataDocument();
orderc = user.SelectedItem.Value.ToString();
mydata.DataSet.ReadXml(Server.MapPath("OrderGuides/" + orderc +
".xml"));
return mydata.DataSet;
}


void CreateXML()
{
DataSet objdata = new DataSet("IndianMaster");
DataTable dt = new DataTable("Product");
DataRow dr;
orderc = user.SelectedItem.Value.ToString();
dt.Columns.Add("NAME");
dt.Columns.Add("SKU");
dt.Columns.Add("PRICE");
dt.Columns.Add("TYPE");
dr = dt.NewRow();
dr[0] = "No Data";
dr[1] = "No Data";
dr[2] = "No Data";
dr[3] = "No Data";
dt.Rows.Add(dr);
objdata.Tables.Add(dt);
datagrid1.DataSource=objdata;
datagrid1.DataBind();
objdata.WriteXml(Server.MapPath("/Secured/XML/"+orderc+".xml"));
LoadXML();

}

public void doEdit (Object sender, DataGridCommandEventArgs e)
{
datagrid1.EditItemIndex = e.Item.ItemIndex;
LoadXML();
}
 
M

manmit.walia

Thanks David...
I will take a look at your code and compare it too mine.

Thanks agian for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top