Tuesday, May 22, 2012

sql server:how to create a foreign key relationship

Here i will show how to create a foreign  key relation between two tables in server.The parent table must have the primary key field when we will giving the foreign key with child table.For this we have to right click on the child table ,select relationships then  we will get a pop like as in screen.In this pop up we have an option "Table and column specification" ,,then we have to select circled option  which is right to blue color field.
We will get an option to select which  tables has to be involved in  foreign key relation as in below screen.Here the emp ,table_3 are table's are used
 The ID is the primary key field in emp table is foreign key relation with id in table_3 table.we can see this process as in below 2 images

After all process has done ,we can see the which table and columns are involved in this relationships

Monday, May 21, 2012

How to programmatically download file from sharepoint

  Here i will show how to get the documents from share point site.For this we can use path of document to download or to do any file operation in folder.The below example has the path of the url of document(testfilename) i:e http://www.localwebtest.net/TestDownloadimageSP/Document/results.doc".If you  what to get the data we should use the web client class.For this we have to add the System.Net.Web Client name space
WebClient webc = new WebClient();
webc.UseDefaultCredentials = true;
byte[] testbytedata = webc.DownloadData(path);
testfilename="pathofdocumnet";
FileStream fst = new FileStream(testfilename, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter biw = new BinaryWriter(fst);
biw.Write(testbytedata);

create custom list view and filter the list data in sharepoint 2010

SharePoint has one of the best feature i:e we can  create a custom  view for list.Basically the view can used to display the user tasks based on the login in share point and we can filter the list data using as our filter condition.Here i will show how to create a view for subjects list and filter the data as per author
using (SPSite Site = new SPSite(url))
{
using (SPWeb sWeb = Site.OpenWeb())
{
SPList sList = sWeb.Lists["subjects"];
SPViewCollection sViewCollection = sList.Views;
string testViewName = "TestView";
System.Collections.Specialized.StringCollection testviewFields = new System.Collections.Specialized.StringCollection();
testviewFields.Add("Id");
testviewFields.Add("Name");
testviewFields.Add("Author");
testviewFields.Add("Group");
string spquery = "<query><where><eq><fieldref name="\"Name\"/">" +
                    "<value type="\"author\"/">Testview</value></fieldref></eq></where></query>";
sViewCollection.Add(testViewName, testviewFields, spquery, 100, true, false);
sWeb.Update();
}
}

Grid view row data bound event in asp.net

Here I will show how to do RowDataBound event in grid view in asp.net.For this I have given a simple example which is bind the percentage data to label in grid view.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="lblstudentid" runat="server" Text='<%#Eval("StudentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblstudentname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%#Eval("total")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage">
<ItemTemplate>
<asp:Label ID="lblPercentage" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>   
</Columns>
</asp:GridView>
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
 Label lblPercentage = (Label)e.Row.FindControl("lblPercentage");
 Label lblTotal = (Label)e.Row.FindControl("lblTotal");
 if (total > 0)
 {
 float percentage = float.Parse(lblTotal.Text) / total;
 percentage = percentage * 100;
 lblPercentage.Text = percentage.ToString();
 }
 else
 {
 lblPercentage.Text = "0";
 }
 }
 }

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
}
}

Bel