We see bulk insert by taking 1 example :
Create 1 table into Database
For ex., Database Name : Employee
Create Table :- EmpInfo(EmpId,EmpName,Address,City)
After making this goto .aspx.cs page and create 1 datatable in which you can insert data .
Step 1 :
//Create Datatable
void CreateDT()
{
DataTable dt = new DataTable("Emp");
dt.Columns.Add("EmpCode");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Address");
ViewState["dt"] = dt;
}
Step 2 : Create simple form to take employee information from user
step 3: Put add button
Step 4 : Add Grid in Page to display record of DataTable
Step 5 : write below code on btnAdd_Click() event to add form data into DataTable
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
DataRow dr = dt.NewRow();
dr["EmpCode"]=Convert.ToInt64(TextBox1.Text);
dr["FirstName"] = TextBox2.Text;
dr["LastName"] = TextBox3.Text;
dr["Address"] = TextBox4.Text;
dt.Rows.Add(dr);
ViewState["dt"] = dt;
BindGrid();
}
Step 6 : Put 1 more button to test Bulk Insert
Write the below code in the btnBulkInsert_Click() event to Insert records of DataTable into EmpInfo Table
//'EmployeeConnectionString' is the connection string name which is defined in web.config file
protected void btnBulkInsert_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["EmployeeConnectionString"].ToString());
SqlBulkCopy bulkcopy = new SqlBulkCopy(con1);
bulkcopy.DestinationTableName = "EmpInfo";
con1.Open();
//For Map the DataTable columns with Sql Server Table
//bulkcopy.ColumnMappings.Add(datatable column,Sql server Column)
bulkcopy.ColumnMappings.Add(0, 0);
bulkcopy.ColumnMappings.Add(1, 1);
bulkcopy.ColumnMappings.Add(2, 3);
bulkcopy.ColumnMappings.Add(3, 4);
bulkcopy.WriteToServer((DataTable)ViewState["dt"]);
con1.Close();
}
Sunday, July 19, 2009
Thursday, July 16, 2009
Check Email format using Javascript
function validatemail()
{
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.form1.txtaltermail.value;
if(reg.test(address) == false)
{
return false;
}
else
{
return(true);
}
}
{
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.form1.txtaltermail.value;
if(reg.test(address) == false)
{
return false;
}
else
{
return(true);
}
}
Check only digits in TExtBox using Javascript
//TextBox indicates the ID of the TextBox in the form
function checknum(TextBox)
{
var str=TextBox.value;
for(i=0;i < str.length;i++)
{
var num=str.charAt(i);
if((num>="0")&&(num<="9"))
{
}
else
{
return(false);
}
}
}
function checknum(TextBox)
{
var str=TextBox.value;
for(i=0;i < str.length;i++)
{
var num=str.charAt(i);
if((num>="0")&&(num<="9"))
{
}
else
{
return(false);
}
}
}
Check only characters in Textbox using Javascript
//TextBox indicates the id of the TextBox in the form
{
function checkcharTXT(TextBox)
var str=TextBox.value;
for(i=0;i < str.length;i++)
{
var strchar=str.charAt(i);
if(((strchar>="a")&&(strchar<="z"))||((strchar>="A")&&(strchar<="Z")))
{
}
else
{
return(false);
}
}
}
{
function checkcharTXT(TextBox)
var str=TextBox.value;
for(i=0;i < str.length;i++)
{
var strchar=str.charAt(i);
if(((strchar>="a")&&(strchar<="z"))||((strchar>="A")&&(strchar<="Z")))
{
}
else
{
return(false);
}
}
}
Check all the Checkboxes in Gridview using Javascript
//where 'Hchkbox' is the id of Checkbox in HeaderTemplate
function SelectAll(Hchkbox)
{
//get reference of DataList/GridView control
var grid = document.getElementById("<%= DataList1.ClientID %>");
var cell;
if (grid.rows.length > 0)
{
//loop starts from 1. rows[0] points to the header.
for (i=1; i < grid.rows.length; i++)
{
//get the reference of first column
cell = grid.rows[i].cells[0];
//loop according to the number of childNodes in the cell
for (j=0; j < cell.childNodes.length; j++)
{
//if childNode type is CheckBox
if (cell.childNodes[j].type =="checkbox")
{
//assign the status of the Select All checkbox to the cell checkbox within the grid
cell.childNodes[j].checked = Hchkbox.checked;
}
}
}
}
}
function SelectAll(Hchkbox)
{
//get reference of DataList/GridView control
var grid = document.getElementById("<%= DataList1.ClientID %>");
var cell;
if (grid.rows.length > 0)
{
//loop starts from 1. rows[0] points to the header.
for (i=1; i < grid.rows.length; i++)
{
//get the reference of first column
cell = grid.rows[i].cells[0];
//loop according to the number of childNodes in the cell
for (j=0; j < cell.childNodes.length; j++)
{
//if childNode type is CheckBox
if (cell.childNodes[j].type =="checkbox")
{
//assign the status of the Select All checkbox to the cell checkbox within the grid
cell.childNodes[j].checked = Hchkbox.checked;
}
}
}
}
}
Saturday, July 11, 2009
Subscribe to:
Posts (Atom)