Tuesday, May 8, 2012

Copy data from one data table to another data table in asp.net

Title:How to copy data from one table to other table in asp.net

Description:

We have two different methods two copy the data form one data table to another data table .First one is to add the rows directly from one to other,second one is using Import Row method.In first we get the copied data into data row array,then it will go thorough for each row and add to another data table

DataRow[] selectedrows = Orders.Select("Customer = 'Bhaskar'");
DataRow AddNewRow;
foreach (DataRow row in selectedrows)
{
AddNewRow = CustomersDetails.NewRow();
AddNewRow["customername"] = row["customer"];
CustomersDetails.Rows.Add(AddNewRow);
}
ImportRow() Method:
By using this method we just import  the rows   into destination data table from source data table.
DataTable Orders;
DataTable Ordersdetails;
DataRow[] Rows =  Orders.Select("city='Hyd'");
foreach (DataRow Row in Rows)
Ordersdetails.ImportRow(Row);

Monday, May 7, 2012

How to manipulate the gridview cell

Here i will show how to manipulate the grid view cell styles in windows forms application.For this i will get all grid view rows and compare the grid view header cell value with drop down selected value. Then the related data cells back colour will be changed to Green.In foreach loop we have one more for loop to get the entire column cells of related header in grid view.

foreach(DataGridViewRow row in this.Gvorders.Rows)
{
if (row.HeaderCell.Value == txtorder.Text)
{
for (int i = 0; i < Gvorders.Columns.Count; i++)
{
this.Gvorders.CurrentCell = row.Cells[i];
row.Cells[i].Style.BackColor = Color.Green;
}
}
}
VB:
For Each row As DataGridViewRow In Me.Gvorders.Rows
 If row.HeaderCell.Value = txtorder.Text Then
  For i As Integer = 0 To Gvorders.Columns.Count - 1
   Me.Gvorders.CurrentCell = row.Cells(i)
   row.Cells(i).Style.BackColor = Color.Green
  Next
 End If
Next

Sunday, May 6, 2012

Exception handling in Asp.net

Title:How to use Exception handling in asp.net using c#.net

Description:
To handle the exceptions we are provided two different blocks of code known as key,catch blocks which should be used as following

Try
{
Statements which may cause you the exception
Statements which are related with the exception
}
catch(<exception><obj>
{
Statements that should execute when exception occurs(error message)
}
Once we enclose the code under try and catch blocks the execution of the programs will be as following
If all statements under try are executed successfully,the control jumps to the end of the catch block with out executing the catch blocks
If any of the statement under try executes an exception,the control from that hold the exception object or not,if they control hold abnormal termination occurs
using System

Class trycatchdemo
{
static void main()
{
int x,y,z;
try
{
console.write("Enter X value");
x=int.parse(console.ReadKey());
console.Write("enter y value");
y=int.parse(console.ReadKey());
z=x/y;
console.Write("The result is" +z););
}
catch (DivideByZeroException ex)
{
console.Write("Can not divide by zero");
}
catch(FromatException ex1)
{
console.Write("Can not divide by char");
}
catch(exception ex2)
{
console.Write("error occur");
}
}
}
}
}

Friday, May 4, 2012

how to add a list item into a list to sharepoint site in sharepoint 2010

Here i  will show  how to add a item to a list dynamically to share point root site.Then We have to get  the share point root site then add the items to list.The existing lists  will get by using GetList()  method .Finally we have to submit the changes of data context.

using (var dataContext = new MySharePointSite1L2spDataContext("http://localhost/test"))
{
  var Existingitems = dataContext.GetList<Item>("ItemMain");  
  Item Nitem = new Item();
  Nitem.Title = "Item for test";
  Nitem.Path = "/Lists/ItemMain";  
  
  Existingitems.InsertOnSubmit(Nitem);
  dataContext.SubmitChanges();
}


Thursday, May 3, 2012

How to binding the data to a chart in asp.net

Title:How to bind data to chart control in asp.net using c#.net

Description:
Binding data to a chart control is pretty simple in asp.net.The below example will show how to set the  student marks and exam types.For this two arrays String[],Double[] are used to get the count of class subject and exams which are given to chart x,y axis values
String[] sItems = new String[10];
Double[] iValue = new Double[10];
int j;
string s = "select count (*) as count from class_subject where classid='4'";
Con.Open();
cmd = new SqlCommand(s, Con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
count = int.Parse(dr["count"].ToString());
}
Con.Close();
string sql = "select count (*) as count from exams e, marks m
where m.examid=e.examid and m.admissionno='" +
Session["admissionnumber"] + "'";
Con.Open();
cmd = new SqlCommand(sql, Con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
exams = int.Parse(dr["count"].ToString());
}
Con.Close();
string sql1 = "select e.examtype as examname,ms.marks as obtainedmarks,mm.maximumfrom  marks_subject ms,marks m,exams
e,maxmarks mm  where e.examid=m.examid and  m.marksid=ms.marksid and
e.examid=mm.examid and  m.admissionno='" + Session["admissionnumber"]
+ "' and ms.subjectid='17'";
Con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql1, Con);
Con.Close();
ds.Tables.Clear();
da.Fill(ds);
for (j = 0; j < exams; j++)
{
sItems[j] = ds.Tables[0].Rows[j][0].ToString();
obt = int.Parse(ds.Tables[0].Rows[j][1].ToString());
max = int.Parse(ds.Tables[0].Rows[j][2].ToString());
totalmarks = count * max;
percentage = (double)obt / totalmarks * 100;
string rounded = percentage.ToString("#0.00");
iValue[j] = double.Parse(rounded.ToString());
}
sItems[j] = "maximum % ";
iValue[j] = 100;
// axis values
markschart.YAxisValues = iValue;
markschart.YAxisItems = sItems;
markschart.Visible = true;
markschart.ChartTitle = "Marks chart:";
markschart.XAxisTitle = "(units display percentage)";

Bel