Tuesday, April 26, 2011

Copy tables from one database to another in sql

SELECT *
INTO Destinationtdb.dbo.MyTable
FROM Source.dbo.MyTablejavascript:void(0)

Select one field of many

function selectone(objSource, objArgs) {

var fldvalid;
var first = document.getElementById('<%= flImage.ClientID %>')
var second = document.getElementById('<%=txtembed.ClientID %>')
var third = document.getElementById('<%=FileUpload2.ClientID %>')
if (first.value == "" && second.value == "" && third.value == "") {
fldvalid = false;
}
else if (first.value != "" && second.value != "" && third.value != "") {
fldvalid = false;
}
else if (first.value != "" && second.value != "")
fldvalid = false;
else if (first.value != "" && third.value != "")
fldvalid = false;
else if (second.value != "" && third.value != "")
fldvalid = false;
else {
fldvalid = true;
}
objArgs.IsValid = fldvalid;
return;
}


asp:CustomValidator ID="CustomValidator2" ValidationGroup="News" runat="server"
ClientValidationFunction="selectone" Display="None"
ValidateEmptyText="true" ErrorMessage="Required One video path or embed or image." asp:CustomValidator

check file extension through javascript

.aspx
function checkFileExtension(objSource, objArgs) {
var blnValid;
var file = objArgs.Value.split(".");
var extn = file[file.length - 1].toUpperCase();

if (extn == "MOV" || extn== "WMV") {
blnValid = true;

}
else {
blnValid = false;
}
objArgs.IsValid = blnValid;
return;
}

asp:CustomValidator ID="CustomValidator1" ValidationGroup="News" runat="server"
ClientValidationFunction="checkFileExtension" ControlToValidate="flImage" Display="None"
ValidateEmptyText="false" ErrorMessage="Only .mov,.wmv video files are allowed." asp:CustomValidator

Check date format in textbox by compare validator

asp:CompareValidator ID="CompareValidator1" runat="server" Display="None"
ControlToValidate="txtDate" ErrorMessage="Invalid Date" ValidationGroup="News"
Operator="DataTypeCheck" Type="Date"
asp:CompareValidator

select max value of field by linq

int clientid;
var max_Query = (from c in abc.Table where (c.id== clientid)
select c.SortOrder
).Max();

Add To Cart funtionality

private void GetCartData()
{

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ProductId", typeof(int)));
dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
dt.Columns.Add(new DataColumn("ProductOption3Value", typeof(string)));
dt.Columns.Add(new DataColumn("ProductOption2Value", typeof(string)));
dt.Columns.Add(new DataColumn("ProductOption1Value", typeof(string)));
dt.Columns.Add(new DataColumn("SalePrice", typeof(int)));
dt.Columns.Add(new DataColumn("TotalPrice", typeof(int)));
dt.Columns.Add(new DataColumn("Quantity", typeof(int)));
dt.Columns.Add(new DataColumn("Thumbnail", typeof(string)));

DataRow dr;
if (ViewState["ProductId"] != null)
productid = Convert.ToInt32(ViewState["ProductId"]);

if (Session["CartData"] == null)
{
dr = dt.NewRow();
//add the row to DataTable
var Pl = mse.ProductDesc.Where(c => c.ProductId == productid).FirstOrDefault();
if (Pl != null)
{
//add values to each rows
dr["Productid"] = Pl.ProductId;
dr["ProductName"] = Pl.ProductName;
dr["SalePrice"] = Pl.Saleprice;
dr["TotalPrice"] = Pl.Saleprice * 1; //Convert.ToInt32(ddlqty.SelectedItem.Value);
dr["Quantity"] = 1;// ddlqty.SelectedItem.Value;
dr["Thumbnail"] = Pl.Thumbnail;
dt.Rows.Add(dr);
}
}
else
{
var Pl = mse.ProductDesc.Where(c => c.ProductId == productid).FirstOrDefault();
if (Pl != null)
{
dt = (DataTable)Session["CartData"];

dr = dt.NewRow();
dr["Productid"] = Pl.ProductId;
dr["ProductName"] = Pl.ProductName;
//dr["ProductOption3Value"] = ddlsize.SelectedItem.Text;
//dr["ProductOption2Value"] = Pl.ProductOption2Value;
//dr["ProductOption1Value"] = Pl.ProductOption1Value;
dr["SalePrice"] = Pl.Saleprice;
dr["TotalPrice"] = Pl.Saleprice * 1;// Convert.ToInt32(ddlqty.SelectedItem.Value);
dr["Quantity"] = 1;// ddlqty.SelectedItem.Value;
dr["Thumbnail"] = Pl.Thumbnail;
dt.Rows.Add(dr);
}
}
Session["CartData"] = dt;
Response.Redirect("~/viewcart.aspx");

}


//second page
protected DataTable GetCartData()
{
DataTable dt;
dt = (DataTable)Session["CartData"];
for (int intC = 0; intC < dt.Rows.Count; intC++)
{
if (mgvBasket.Rows[intC].RowType == DataControlRowType.DataRow)
{
if (((CheckBox)mgvBasket.Rows[intC].FindControl("mchkRemove")).Checked)
{
dt.Rows[intC].Delete();

}
else if (((CheckBox)mgvBasket.Rows[intC].FindControl("mchkRemove")).Checked || ((TextBox)mgvBasket.Rows[intC].FindControl("mtxtQty")).Text != "")
{
string strqty = ((TextBox)mgvBasket.Rows[intC].FindControl("mtxtQty")).Text;
if (!string.IsNullOrEmpty(strqty))
{
int qty = Convert.ToInt32(strqty);
int saleprice = Convert.ToInt32(dt.Rows[intC]["SalePrice"]);
dt.Rows[intC]["Quantity"] = qty;
dt.Rows[intC]["TotalPrice"] = qty * saleprice;
}
}
else { }

}
else { }
}
dt.AcceptChanges();
GetTotal();
Session["CartData"] = dt;
return dt;
}
public void GetTotal()
{

double Rate = 0;
if (Session["ship"] != null)
{
ShippingInfo ship = (ShippingInfo)Session["Ship"];
// Rate = RateServices.GetRate(ship, 15);
}
DataTable dt = new DataTable();
if (Session["CartData"] != null)
{
dt = (DataTable)Session["CartData"];
int count = 0;

if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
count += Convert.ToInt32(dt.Rows[i]["Quantity"]);

TotalAmt = TotalAmt + Convert.ToInt32(dt.Rows[i]["Quantity"]) * Convert.ToInt32(dt.Rows[i]["SalePrice"]);


}
Session["Items"] = count;
subtotal = TotalAmt;
lblsubtotal.Text = subtotal.ToString("c");
Session["SubTotal"] = subtotal;
Rate = 0;
Session["Rate"] = Rate;
lblshiprate.Text = Rate.ToString("c");
TotalAmt = TotalAmt + Rate;
Session["Total"] = TotalAmt;
//lblrate.Text = Rate.ToString("c");
lblTotal.Text = Convert.ToDouble(TotalAmt).ToString("c");
Session["Total"] =TotalAmt;
}
else
{
Session["CartData"] = null;
Session.Clear();
Response.Redirect("~/index.aspx");
}
}
}

sorting in gridview

protected void BindProductsGrid()
{
DataTable dt =GetAllProducts();
if (dt.Rows.Count > 0)
{
if (ViewState["Sort"] == null)
dt.DefaultView.Sort = "ProductName Asc";
else
dt.DefaultView.Sort = ViewState["Sort"].ToString();

gvProducts.AllowSorting = true;
gvProducts.DataSource = dt.DefaultView;
gvProducts.DataBind();
}
else
{
gvProducts.AllowSorting = false;
DataSetLinqOperators.ShowNoResultFound(dt, gvProducts, "There is no Product Types.");
}
}
public static DataTable GetAllProducts()
{
bhscEntities ese = new bhscEntities();
var pt = (from c in ese.Products
join d in ese.ProductImages on c.ProductId equals d.ProductId
where c.Deleted == false
orderby c.ProductName ascending
select new
{
c.ProductId,
d.Thumbnail,
c.ProductName,
c.ProductOption1Value,
c.ProductOption2Value,
c.ProductOption3Value,
c.SKU,
c.Saleprice
});

DataTable dt = DataSetLinqOperators.CopyToDataTable(pt);
return dt;
}

protected void gvProducts_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["Sort"] != null)
{
if (ViewState["Sort"].ToString().Substring(ViewState["Sort"].ToString().Length - 4, 4).Trim() == "ASC")
ViewState["Sort"] = e.SortExpression + " DESC";
else
ViewState["Sort"] = e.SortExpression + " ASC";
}
else
ViewState["Sort"] = e.SortExpression.ToString() + " Desc";

BindProductsGrid();
}

show and hide buttons or link while printout page

javascript funtion
function ShowHelp(id) {
document.getElementById(id).style.display = "block";
}
function HideHelp(id) {
document.getElementById(id).style.display = "none";
window.print();
setTimeout("ShowHelp('aprint')", 3000);


}
function HideHelp1(id) {
document.getElementById(id).style.display = "none";

setTimeout("ShowHelp('imgbtdelorder')", 3000);


}


calling on button click

a id="aprint" onclick="javascript:HideHelp1('imgbtdelorder');javascript:HideHelp('aprint');" href="JavaScript:window.print();">Print this page a

Saturday, April 23, 2011

2 querysting on postback event of linkbuton

asp:LinkButton ID="lbtprname" runat="server" PostBackUrl='<%# "Description.aspx?Productid="+Eval("ProductId")+"&ProductTypeID="+protypeid %>'