Sunday, May 20, 2012

how to use XmlDataSource in asp.net

Here i will show how to use xml as the data source for user information.
1.For this create an XML file using add new item and enter user information with required columns inside it
2.Got o data layer and write a method to search data in the created XML file and return true or false
3.Go to login page and in logiin button check for credentials using data layer but they time passing XML file as the option.We can write login's event to check in data base or xml.
Add-->newitem-->xml file ->create user.xml
<userInfo>
<user>
<username>bhaskar</username>
<password>bhaskar</password>
</user>
</userInfo>Add-->newitem-->xml file ->create user.xml
<userInfo>
<user>
<username>bhaskar</username>
<password>bhaskar</password>
</user>
</userInfo>

Then in app_code go to data layer
public Datalayer()
public static bool validateuser(string username,string pwd,string loc)
{
Dataset ds=New Dataset();
ds.ReadXML(loc);
DataRow[] recs=ds.Tables[0].select("username="'+uname+'" and password"'+pwd+'"";
if(recs.lenth>0)
trturn true;
else
return false;
}
in web.config
<appsettings>
<add key="loginsrc" value="XML"/>
</appsettings>

sign in code behind
bool found;
if(ConfiguarationManager.Appsettings["loginsrc"]=="XML")
{
found=Datalayer.validateuser(txtuser.Text,txtpwd.Text,server.mappath("user.xml"));
}
else
{
found=Datalayer.validateuser(txtuser.Text,txtpwd.Text)
if(found)
FormsAuthentication.Redirectfromloginpage(txtuser.Text,chk.checked);
else
lbl.text="Invalid username and password"l
}
}

No comments:

Bel