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();
}
}
}
}