473,394 Members | 1,478 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,394 software developers and data experts.

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

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="N AME='" & 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="N AME='" &
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="N AME='" & 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")=tx t1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt 2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=t xt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=tx t4.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="N AME='" &
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();

}
}
}
}

Nov 26 '05 #1
5 2698
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
<ma**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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="N AME='" & 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="N AME='" &
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="N AME='" & 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")=tx t1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt 2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=t xt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=tx t4.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="N AME='" &
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();

}
}
}
}

Nov 26 '05 #2
> 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#.

Go to http://www.aisto.com/roeder/dotnet/ and download Lutz Roeder's excellent
Reflector application. Load your compiled VB.NET assembly and decompile it
to C#.

Anders Norås
http://dotnetjunkies.com/weblog/anoras
Nov 26 '05 #3
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.

Nov 27 '05 #4
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

"ma**********@gmail.com" wrote:
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="N AME='" & 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="N AME='" &
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="N AME='" & 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")=tx t1.Text
objdata.Tables("Product").Rows(v1).Item("SKU")=txt 2.Text
objdata.Tables("Product").Rows(v1).Item("PRICE")=t xt3.Text
objdata.Tables("Product").Rows(v1).Item("TYPE")=tx t4.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="N AME='" &
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();
}

Nov 27 '05 #5
Thanks David...
I will take a look at your code and compare it too mine.

Thanks agian for your help.

Nov 28 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
22
by: OHM \( Terry Burns \) | last post by:
I was thinking about community the other day, and wondering what would be the holy grail for a developer in need. I mean, we do get help here, but its done on a best endeavours basis and its really...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
3
by: ravi | last post by:
Hi, I am new to C++ .Can anyone please tell me what does ":" operator mean in C++.For ex: there is a program snippet which looks like that class convert { protected: double val1; // initial...
11
by: Matthew | last post by:
Ok let me try to explain this as good as I can. I am creating this application where it contains a userlogin class. The user logs in before entering the main apploication. I want to do audit trails...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
0
by: Pucca | last post by:
Hi I'm using vs2005. I am getting a bunch of compiler warnings after I made some changes to my code that was compiling clean. I'm also getting memory errors when I run my program and it's...
8
by: SanjaiGandhi | last post by:
Hi ...i am new to programing....pls help to overcome this program.. The Program is..: if a = 557..using for loop or while or dowhile ..we have to get the answer for 5+5+7..that is what ever...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.