.NET

Hi Friends, Post here your .NET Question to get solution.
.


How to Select/Delete Multiple Rows from Gridview

Code Behind :

public partial class Select_Multiple : System.Web.UI.Page
{
static int count=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.AutoGenerateColumns = false;
this.GetData();
}
BtnDel.Attributes.Add("onclick",
"return confirm('Are you sure you want to delete selected item(s) ?');");
}
private void GetData()
{

DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString()))
{
// write the sql statement to execute
string sql = "SELECT * FROM employees";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
}

}
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}


protected void Chkall_Checked(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (Chkall.Checked == true)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
chk.Checked = true;
}
else
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
chk.Checked = false ;
}
}
}

protected void BtnDel_Click(object sender, EventArgs e)
{
ArrayList productsToDelete = new ArrayList();

foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{

CheckBox chk = (CheckBox)row.FindControl("chkSelect");
//if (chkDelete != null)
//{
if (chk.Checked==true )
{
count = count + 1;
string productId = row.Cells[2].Text;
productsToDelete.Add(productId);
// Delete record from Database according to ArrayList
// You can pass it to Another Table to keep track
// Then GetData();

//}
}
}
}

//DeleteProducts(productsToDelete);

Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Total Rows Deleted-" + count + " ' )", true);

}
protected void BtnSelect_Click(object sender, EventArgs e)
{
//Just for EVENT FIRING
}

PAGE SOURCE :


How to Login in Website without Data Base Concept

Here i am using .txt file for storing Login Credential with |(Bar seperator) you can use any special character for that.That file is inside your application. you can do Complete Login Concept with This code. No need any database.
Uid & Pwd Format=sa|123

Code Behind
String contents;
StreamReader sr;
static int count;
protected void Page_Load(object sender, EventArgs e)
{
count=0;
}
protected void Btnlog_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Server.MapPath("LoginCredentials.txt")))
{
string FILENAME = Server.MapPath("LoginCredentials.txt");
sr = File.OpenText(FILENAME);
contents = sr.ReadToEnd();
if (contents.IndexOf(TxtID.Text.Trim()) > -1)
{
count = count + 1;
}
if (contents.IndexOf(Txtpwd.Text.Trim()) > -1 && count == 1)
{

Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Login Successfully..... ')", true);
}
else
{
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Login Invalid...')", true);
}
}
}
}
How to see Image Preview Before saving it into Data Base

Code Behind :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO ;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
string imgname;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void BtnPreview_Click(object sender, EventArgs e)
{
string L_path = "No Images";
imgname = FileUpload1.FileName;
TextBox1.Text = imgname;

// break the pathe, collect file type then match.
string filetype;
if (filetype.ToLower().ToString() == ".jpg" && filetype.ToLower == ".jpeg" && filetype.ToLower == ".bmp" && filetype.ToLower == ".png")
{

ClientScript.RegisterStartupScript(typeof(string), "You choosen the file in wrong formated.. !!!", "");

}
if (FileUpload1.PostedFile.ContentLength > 5242880) // byte size
{
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Image Size is should be Less Than 5 MB' )", true);

}
if (FileUpload1.HasFile)
{
L_path = Server.MapPath("~/Images/").ToString();
L_path += imgname;
FileUpload1.SaveAs(L_path); // Temporary saved

}
else
{
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('No Image is Selected..)", true);

}
Image1.ImageUrl = "~/Images/" + imgname;

Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('Image is Reflected..Plz Save it)", true);

// After saving into Database delete current file from Application.
}

}

4 comments:

  1. How to Encode your password while saving into Data Base using C#

    private string base64Encode(string sData)
    {
    try
    {
    byte[] encData_byte = new byte[sData.Length];
    encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
    string encodedData = Convert.ToBase64String(encData_byte);
    return encodedData;
    }
    catch (Exception ex)
    {
    throw new Exception("Error in base64Encode" + ex.Message);
    }
    }


    while saving pass your password value into this function before saving.

    ReplyDelete
  2. How to bind Listbox from SQL DATA

    da = new SqlDataAdapter(str1, con);
    da.Fill(dt1);

    Lst1.DataSource=dt.DefaultView;
    Lst1.DataTextField = "Column Name";
    Lst1.DataValueField = "Column Name";
    Lst1.DataBind();

    ReplyDelete
  3. LowerCase to UpperCase while writing in ASP.NET with c#


    function changeToUpperCase(controlName)
    {
    document.getElementById(controlName).value = document.getElementById(controlName).value.toUpperCase();
    }


    ReplyDelete
  4. LowerCase to UpperCase while writing in ASP.NET with c#

    function changeToUpperCase(controlName)
    {
    document.getElementById(controlName).value = document.getElementById(controlName).value.toUpperCase();
    }
    On TextBox Control:
    ID="controlName" runat="server" onKeyPress="changeToUpperCase(this.id)"

    ReplyDelete