Purpose: How to use FileUploadControl Inside Update Pannel ASp.Net
.aspx page
------------------
.cs file
-----------
protected void SubmitButton_Click(object sender, EventArgs e)
{
Label SubmitButtonLabel2 = (Label)UpdatePanel1.FindControl("SubmitButtonLabel");
if (FileUpload1.HasFile)
{
string[] fileName = FileUpload1.FileName.Split('.');
string _extension = fileName[fileName.Length - 1];
if (_extension == "jpg" || _extension == "gif" || _extension == "bmp" || _extension == "png")
{
string _filepath = Server.MapPath("~/UPLOADIMAGE");
FileUpload1.PostedFile.SaveAs(_filepath + "\\" + FileUpload1.FileName);
SubmitButtonLabel2.Text = "File Accepted.";
}
else
{
SubmitButtonLabel2.Text = "File type not allowed. Please choose another.";
}
}
else
{
SubmitButtonLabel.Text = "Please select a file.";
}
}
.Net help for New Comer
This Blog for those guys who are new to .Net Technology..
Friday, August 28, 2009
Friday, July 17, 2009
Show the gridview header and footer when there is no records in database table
Purpose: How to show the gridview header and footer when there is no records in database table
Controls: Griedview
Job: Add TextBoxes to GridView Footer and apply property of GriedView showFooter=True;
Connection Object...
SqlConnection con = new SqlConnection("Data Source=SUSANT\\SQLEXPRESS ;Initial Catalog=Infitech; Integrated Security=True;");
DataTable dt = new DataTable();
//PageLoad Event..
protected void Page_Load(object sender, EventArgs e)
{
show_data();
}
// show_data() method to select all the values from database
public void show_data()
{
try
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
string str = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(ds, "temp");
da.Fill(dt);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
else
{
ShowNoResultFound(dt,GridView1);
}
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
}
ShowNoResultFound() it will check if no record then it will create one error message...
private void ShowNoResultFound(DataTable source, GridView gv)
{
source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
// Bind the DataTable which contain a blank row to the GridView
gv.DataSource = source;
gv.DataBind();
// Get the total number of columns in the GridView to know what the Column Span should be
int columnsCount = gv.Columns.Count;
gv.Rows[0].Cells.Clear();// clear all the cells in the row
gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
gv.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
//Output if there is no record.....

Controls: Griedview
Job: Add TextBoxes to GridView Footer and apply property of GriedView showFooter=True;
Connection Object...
SqlConnection con = new SqlConnection("Data Source=SUSANT\\SQLEXPRESS ;Initial Catalog=Infitech; Integrated Security=True;");
DataTable dt = new DataTable();
//PageLoad Event..
protected void Page_Load(object sender, EventArgs e)
{
show_data();
}
// show_data() method to select all the values from database
public void show_data()
{
try
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
string str = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(ds, "temp");
da.Fill(dt);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
else
{
ShowNoResultFound(dt,GridView1);
}
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
}
ShowNoResultFound() it will check if no record then it will create one error message...
private void ShowNoResultFound(DataTable source, GridView gv)
{
source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
// Bind the DataTable which contain a blank row to the GridView
gv.DataSource = source;
gv.DataBind();
// Get the total number of columns in the GridView to know what the Column Span should be
int columnsCount = gv.Columns.Count;
gv.Rows[0].Cells.Clear();// clear all the cells in the row
gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
gv.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
//Output if there is no record.....
Highlight Gridview row when mouse hover.
purpose: when mouse hover to girview row it will highlight the row..
controls: Add one gridview
connection object....
SqlConnection con = new SqlConnection("Data Source=SUSANT\\SQLEXPRESS ;Initial Catalog=Infitech; Integrated Security=True;");
//Page Load Event...
protected void Page_Load(object sender, EventArgs e)
{
show_data();
}
//select all the records from database..
public void show_data()
{
try
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
string str = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(ds, "temp");
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
//gridview RowCreated Event....
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
string onmouseoverStyle = "this.style.backgroundColor='cyan'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", onmouseoverStyle);
e.Row.Attributes.Add("onmouseout", onmouseoutStyle);
}
}
controls: Add one gridview
connection object....
SqlConnection con = new SqlConnection("Data Source=SUSANT\\SQLEXPRESS ;Initial Catalog=Infitech; Integrated Security=True;");
//Page Load Event...
protected void Page_Load(object sender, EventArgs e)
{
show_data();
}
//select all the records from database..
public void show_data()
{
try
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
string str = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(ds, "temp");
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
//gridview RowCreated Event....
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
string onmouseoverStyle = "this.style.backgroundColor='cyan'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", onmouseoverStyle);
e.Row.Attributes.Add("onmouseout", onmouseoutStyle);
}
}
Subscribe to:
Posts (Atom)