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 = CustomerDropDow n.SelectedItem. Value.ToString
temp.Text = customer
Try
objdata.ReadXML (Server.Mappath ("/Secured/XML/"+customer+".xm l"))
datagrid1.Datas ource=objdata
datagrid1.Datab ind()
Catch
CreateXML
End Try
End sub
Sub CreateXML
Dim objdata as New DataSet("Indian Master")
Dim dt as New DataTable("Prod uct")
Dim dr as DataRow
Dim customer as String
customer = CustomerDropDow n.SelectedItem. Value.ToString
temp.Text = customer
dt.Columns.Add( New DataColumn("NAM E"))
dt.Columns.Add( New DataColumn("SKU "))
dt.Columns.Add( New DataColumn("PRI CE"))
dt.Columns.Add( New DataColumn("TYP E"))
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.datas ource=objdata
datagrid1.datab ind()
objdata.WriteXM L(Server.mappat h("/Secured/XML/"+customer+".xm l"))
LoadXML()
End Sub
Sub setEditMode(Sen der as Object, E as DataGridCommand EventArgs)
Dim objdata as New DataSet
Dim x1 as string
Dim customer as String
customer = CustomerDropDow n.SelectedItem. Value.ToString
temp.Text = customer
objdata.ReadXML (Server.Mappath ("/Secured/XML/"+customer+".xm l"))
x1=datagrid1.Da taKeys.Item(e.I tem.ItemIndex)
objdata.Tables( "Product").Defa ultView.RowFilt er="NAME='" & x1 &
"'"
If objdata.Tables( "Product").Defa ultView.Count>0 Then
error4.visible= "False"
datagrid1.EditI temIndex=E.Item .ItemIndex
datagrid1.showf ooter="false"
LoadXML
Else
error4.visible= "True"
End If
End sub
Sub cancelEdit(send er as Object, E as DataGridCommand EventArgs)
datagrid1.EditI temIndex=-1
datagrid1.showf ooter="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 DataGridCommand EventArgs)
Dim customer as String
customer = CustomerDropDow n.SelectedItem. Value.ToString
temp.Text = customer
If e.CommandName=" Delete" Then
If datagrid1.EditI temIndex=-1 Then
error5.visible= "False"
Dim x1 as string
x1=datagrid1.Da taKeys.Item(e.I tem.ItemIndex)
Dim objdata as New DataSet
Try
objdata.ReadXML (Server.Mappath ("/Secured/XML/"+customer+".xm l"))
objdata.Tables( "Product").Defa ultView.RowFilt er="NAME='" &
x1 & "'"
If objdata.Tables( "Product").Defa ultView.Count>0 Then
objdata.Tables( "Product").Defa ultView.Delete( 0)
End If
objdata.Tables( "Product").Defa ultView.RowFilt er=""
objdata.WriteXM L(Server.mappat h("/Secured/XML/"+customer+".xm l"))
Catch
CreateXML
End Try
LoadXML
Else
error5.visible= "True"
End If
End if
End Sub
Sub UpdateXML(s as Object, e as DataGridCommand EventArgs)
Dim customer as String
customer = CustomerDropDow n.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+".xm l"))
v1=e.Item.ItemI ndex
x1=datagrid1.Da taKeys.Item(e.I tem.ItemIndex)
objdata.Tables( "Product").Defa ultView.RowFilt er="NAME='" & x1 &
"'"
If objdata.Tables( "Product").Defa ultView.Count>0 Then
error3.visible= "False"
txt1=e.Item.Fin dControl("name_ edit")
txt2=e.Item.Fin dControl("sku_e dit")
txt3=e.Item.Fin dControl("price _edit")
txt4=e.Item.Fin dControl("type_ edit")
objdata.Tables( "Product").Rows (v1).Item("NAME ")=txt1.Tex t
objdata.Tables( "Product").Rows (v1).Item("SKU" )=txt2.Text
objdata.Tables( "Product").Rows (v1).Item("PRIC E")=txt3.Tex t
objdata.Tables( "Product").Rows (v1).Item("TYPE ")=txt4.Tex t
datagrid1.datas ource=objdata
datagrid1.datab ind()
objdata.WriteXM L(Server.mappat h("/Secured/XML/"+customer+".xm l"))
datagrid1.EditI temIndex=-1
LoadXML
datagrid1.showf ooter="true"
Else
error3.visible= "True"
End If
End If
End sub
Sub doInsert(s as Object, E as DataGridCommand EventArgs)
Dim customer as String
customer = CustomerDropDow n.SelectedItem. Value.ToString
temp.Text = customer
if e.CommandName=" doAdd" Then
Dim v1 as string
Dim tadd1,tadd2,tad d3,tadd4 as Textbox
Dim objdata as New DataSet
Dim dr as DataRow
objdata.ReadXML (Server.Mappath ("/Secured/XML/"+customer+".xm l"))
tadd1=e.Item.Fi ndControl("name _add")
tadd2=e.Item.Fi ndControl("sku_ add")
tadd3=e.Item.Fi ndControl("pric e_add")
tadd4=e.Item.Fi ndControl("type _add")
Try
'tadd1.text=int 32.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").Defa ultView.RowFilt er="NAME='" &
tadd1.Text & "'"
If objdata.Tables( "Product").Defa ultView.Count<= 0 Then
error1.visible= "False"
objdata.Tables( "Product").Defa ultView.RowFilt er=""
dr=objdata.Tabl es("Product").N ewRow()
dr(0)=tadd1.Tex t
dr(1)=tadd2.Tex t
dr(2)=tadd3.Tex t
dr(3)=tadd4.Tex t
objdata.Tables( "Product").Rows .Add(dr)
objdata.WriteXM L(Server.mappat h("/Secured/XML/"+customer+".xm l"))
LoadXML
Else
error1.visible= "True"
error1.Text="Pr oduct must be unique"
End If
End If
End If
End Sub
Here is my C# Code:
void LoadXML()
{
DataSet objdata = new DataSet();
orderc = user.SelectedIt em.Value.ToStri ng();
objdata.ReadXml (Server.MapPath ("OrderGuide s/" + orderc + ".xml"));
datagrid1.DataS ource = objdata;
datagrid1.DataB ind();
}
private DataSet CreateDataSourc e()
{
XmlDataDocument mydata = new XmlDataDocument ();
orderc = user.SelectedIt em.Value.ToStri ng();
mydata.DataSet. ReadXml(Server. MapPath("OrderG uides/" + orderc +
".xml"));
return mydata.DataSet;
}
void CreateXML()
{
DataSet objdata = new DataSet("Indian Master");
DataTable dt = new DataTable("Prod uct");
DataRow dr;
orderc = user.SelectedIt em.Value.ToStri ng();
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.DataS ource=objdata;
datagrid1.DataB ind();
objdata.WriteXm l(Server.MapPat h("/Secured/XML/"+orderc+".xml" ));
LoadXML();
}
public void doEdit (Object sender, DataGridCommand EventArgs e)
{
datagrid1.EditI temIndex = e.Item.ItemInde x;
LoadXML();
}
public void doCancel(Object sender, DataGridCommand EventArgs e)
{
datagrid1.EditI temIndex = -1;
LoadXML();
}
public void DelXML (object sender, DataGridCommand EventArgs e)
{
orderc = user.SelectedIt em.Value.ToStri ng();
if(e.CommandNam e == "Delete")
{
if(datagrid1.Ed itItemIndex == -1)
{
string x1;
x1 = datagrid1.DataK eys[e.Item.ItemInde x].ToString();
DataSet objdata = new DataSet();
try
{
objdata.ReadXml (Server.MapPath ("OrderGuide s/" + orderc +
".xml"));
objdata.Tables["Product"].DefaultView.Ro wFilter="NAME=' " + x1 +
"'";
if(objdata.Tabl es["Product"].DefaultView.Co unt > 0)
{
objdata.Tables["Product"].DefaultView.De lete(0);
}
objdata.Tables["Product"].DefaultView.Ro wFilter = "";
objdata.WriteXm l(Server.MapPat h("/Secured/XML/"+orderc+".xml" ));
}
catch
{
}
finally
{
CreateXML();
}
LoadXML();
}
}
}
public void doUpdate(Object sender, DataGridCommand EventArgs e)
{
orderc = user.SelectedIt em.Value.ToStri ng();
string N = ((TextBox)e.Ite m.Cells[0].Controls[1]).Text;
string S = ((TextBox)e.Ite m.Cells[1].Controls[1]).Text;
string P = ((TextBox)e.Ite m.Cells[2].Controls[1]).Text;
string T = ((TextBox)e.Ite m.Cells[3].Controls[1]).Text;
datagrid1.EditI temIndex = -1;
DataSet ds = CreateDataSourc e();
DataRow row = ds.Tables[0].Rows[e.Item.ItemInde x];
row["NAME"]=N;
row["SKU"]=S;
row["PRICE"]=P;
row["TYPE"]=T;
ds.WriteXml(Ser ver.MapPath("/Secured/XML/"+orderc+".xml" ));
//mygrid.DataSour ce = CreateDataSourc e();
LoadXML();
}
public void doInsert (object sender, DataGridCommand EventArgs e)
{
orderc = user.SelectedIt em.Value.ToStri ng();
if(e.CommandNam e == "doAdd")
{
//string v1;
TextBox tadd1,tadd2,tad d3,tadd4 = new TextBox();
DataSet objdata = new DataSet();
DataRow dr;
objdata.ReadXml (Server.MapPath ("OrderGuide s/" + orderc + ".xml"));
tadd1 = (TextBox)e.Item .FindControl("n ame_add");
tadd2 = (TextBox)e.Item .FindControl("s ku_add");
tadd3 = (TextBox)e.Item .FindControl("p rice_add");
tadd4 = (TextBox)e.Item .FindControl("t ype_add");
if(tadd1.Text != "" && tadd2.Text != "" && tadd3.Text != "" )
{
objdata.Tables["Product"].DefaultView.Ro wFilter="NAME=' " +
tadd1.Text + "'";
if(objdata.Tabl es["Product"].DefaultView.Co unt <= 0)
{
objdata.Tables["Product"].DefaultView.Ro wFilter = "";
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.WriteXm l(Server.MapPat h("/Secured/XML/"+orderc+".xml" ));
LoadXML();
}
}
}
}