C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Working with XML in a Datagrid and LoginPage


Posted Date: 13 Nov 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: Gnana Prakash Member Level: Gold    
Rating: 1 out of 5Points: 10



This Project will guide you how to use xml as database, and xml manipulation, datagrid edit and delete function

Login Page Code



protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtloginUser.Value.ToString().Trim() != string.Empty && txtloginPw.Value.ToString().Trim() != string.Empty)
{
CheckLoginValidity();
}
}

protected void CheckLoginValidity()
{
try
{

// Read XML File for Checking userName and Password
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("sample.xml"));
Boolean IsValid = false;

foreach (DataRow dr in ds.Tables[0].Rows)
{
if (!IsValid)
{
if (txtloginUser.Value.ToString().Trim() == dr["name"].ToString() && txtloginPw.Value.ToString().Trim() == dr["pass"].ToString())
{
Session["Authentication"] = "Admin";
Session["Permission"] = dr["permission"].ToString();
string URL = dr["permission"].ToString();
IsValid = true;
//Response.Redirect(URL,false);

}
}
}

if (!IsValid)
{
Session["ErrorMessage"] = "Username and password do not match";
Response.Redirect(Request.ApplicationPath + "//ErrorPage.aspx", false);
}
else
{
Response.Redirect("UserNames.aspx", false);
}


}
catch (Exception ex)
{
Session["ErrorMessage"] = ex.Message.ToString();
Response.Redirect(Request.ApplicationPath + "//ErrorPage.aspx");

}
finally
{


}

}


XML and Datagrid Page
-----------------------------------



public void LoadXML()
{
DataSet objdata = new DataSet();

try
{
objdata.ReadXml(Server.MapPath("sample.xml"));
strCount = objdata.Tables[0].Rows.Count.ToString();
datagrid1.DataSource = objdata;
datagrid1.DataBind();



}

catch
{

CreateXML();
}

}


public void CreateXML()
{
DataSet objdata = new DataSet("root");
DataTable dt = new DataTable("record");
DataRow dr = default(DataRow);

dt.Columns.Add(new DataColumn("aid"));
dt.Columns.Add(new DataColumn("name"));
dt.Columns.Add(new DataColumn("pass"));
dt.Columns.Add(new DataColumn("permission"));

dr = dt.NewRow();
dr[0] = "1";
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("sample.xml"));

LoadXML();
}


public void setEditMode(object Sender, DataGridCommandEventArgs e)
{

DataSet objdata = new DataSet();
string x1;



objdata.ReadXml(Server.MapPath("sample.xml"));

x1 = (string)datagrid1.DataKeys[e.Item.ItemIndex].ToString() ;

objdata.Tables["record"].DefaultView.RowFilter = "aid='" + x1 + "'";

if (objdata.Tables["record"].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)
{
if (e.CommandName == "Delete")
{
if (datagrid1.EditItemIndex == -1)
{
error5.Visible = false;
string x1 = null;

x1 = datagrid1.DataKeys[e.Item.ItemIndex].ToString() ;

DataSet objdata = new DataSet();
try
{
objdata.ReadXml(Server.MapPath("sample.xml"));
objdata.Tables["record"].DefaultView.RowFilter = "aid='" + x1 + "'";
if (objdata.Tables["record"].DefaultView.Count > 0)
{
objdata.Tables["record"].DefaultView.Delete(0);
}

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

objdata.WriteXml(Server.MapPath("sample.xml"));
}
catch
{
CreateXML();
}

LoadXML();
}
else
{
error5.Visible = true;
}
}
}


public void UpdateXML(object s, DataGridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
string str1 = null;
string str2 = null;
string str3 = null;
int v1;
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();

DataSet objdata = new DataSet();
string x1 = null;

objdata.ReadXml(Server.MapPath("sample.xml"));

v1 = (int) e.Item.ItemIndex;

x1 = datagrid1.DataKeys[e.Item.ItemIndex].ToString() ;

objdata.Tables["record"].DefaultView.RowFilter = "aid='" + x1 + "'";

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

txt1 = (TextBox )e.Item.FindControl("name_edit");
txt2 = (TextBox)e.Item.FindControl("pass_edit");
txt3 = (TextBox)e.Item.FindControl("permission_edit");

objdata.Tables["record"].Rows[v1]["name"] = txt1.Text;
objdata.Tables["record"].Rows[v1]["pass"] = txt2.Text;
objdata.Tables["record"].Rows[v1]["permission"] = txt3.Text;
datagrid1.DataSource = objdata;
datagrid1.DataBind();

objdata.WriteXml(Server.MapPath("sample.xml"));

datagrid1.EditItemIndex = -1;
LoadXML();
datagrid1.ShowFooter = true;
}
else
{
error3.Visible = true;
}
}
}


public void doInsert(object s, DataGridCommandEventArgs e)
{

if (e.CommandName == "doAdd")
{
string v1 = null;
TextBox tadd1 = new TextBox();
TextBox tadd2= new TextBox();
TextBox tadd3= new TextBox();
TextBox tadd4 = new TextBox();

DataSet objdata = new DataSet();
DataRow dr = default(DataRow);

objdata.ReadXml(Server.MapPath("sample.xml"));

tadd1 = (TextBox)e.Item.FindControl("aid_add");
tadd2 = (TextBox)e.Item.FindControl("name_add");
tadd3 = (TextBox)e.Item.FindControl("pass_add");
tadd4 = (TextBox)e.Item.FindControl("permission_add");

try
{

error2.Visible = false;

if (Convert.ToInt32(tadd1.Text) < 1)
{
tadd1.Text = "";
error2.Visible = true;
}
}
catch
{
tadd1.Text = "";
error2.Visible = true;
}

if (!string.IsNullOrEmpty(tadd1.Text) & !string.IsNullOrEmpty(tadd2.Text) & !string.IsNullOrEmpty(tadd3.Text) & !string.IsNullOrEmpty(tadd4.Text))
{
objdata.Tables["record"].DefaultView.RowFilter = "aid='" + tadd1.Text + "'";
if (objdata.Tables["record"].DefaultView.Count <= 0)
{
error1.Visible = false;
objdata.Tables["record"].DefaultView.RowFilter = "";
dr = objdata.Tables["record"].NewRow();

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

objdata.WriteXml(Server.MapPath("sample.xml"));
LoadXML();
}
else
{
error1.Visible = true;
error1.Text = "Id must be unique";
}
}
}
}



protected int showval1()
{
int coun = Convert.ToInt32(strCount) + 1;

return coun;
}



Attachments

  • Working with XML in a Datagrid and LoginPage Project (22383-13618-LoginPage.zip)


  • Responses

    Author: krutarth    17 Apr 2009Member Level: Bronze   Points : 1
    hey i m developing windows application using vb.net so please send me code in vb.net ....


    Author: Gnana Prakash     17 Apr 2009Member Level: Gold   Points : 1
    Hi krutarth

    you can use folowing link

    http://articles.techrepublic.com.com/5100-10878_11-5116182.html

    may be it will use full



    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    XML as Database  .  Working with XML in a Datagrid and LoginPage  .  DataGrid Edit and Delete Function  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Dynamically changing row color on mouse over and mouse out
    Previous Resource: To get the repeater item on button click in repeater and delete row from table ion c#
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use