I put some fully codes files that I am trying to make some practices to understand very well the process. Then after clicking to the saving's button. Then when I check to my database it's still blank (there is not datas inside). But whe running the site there is not error message. The only think he doesnt save in my database. What wrong?
.default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
/////////////////////////////////////////
default.asp.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string strcon = "Data Source=localhost\\sqlexpress;Integrated Security=true;Initial Catalog=Mfwamba";
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
SqlCommand cmd;
byte[] raw;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if ((FileUpload1.FileName != ""))
{
//to allow only jpg gif and png files to be uploaded.
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (((extension == ".jpg") || ((extension == ".gif") || (extension == ".png"))))
{
FileStream fs = new FileStream(FileUpload1.PostedFile.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
raw = new byte[fs.Length];
fs.Read(raw, 0, Convert.ToInt32(fs.Length));
con = new SqlConnection(strcon);
cmd = new SqlCommand("prcInsert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@add", TextBox2.Text);
cmd.Parameters.AddWithValue("@photo", raw);
con.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
string script = "";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Added", script);
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
string script = "";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", script);
}
}
else
{
StatusLabel.Text = "Only Jpg,gif or Png files are permitted";
}
}
else
{
StatusLabel.Text = "Kindly Select a File.....";
}
con.Close();
}
catch (Exception e1)
{
}
}
}
///////////////////////////////////
handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
if (context.Request.QueryString["id"] != null)
{
// context.Response.Write(context.Request.QueryString["id"]);
//string dbcon = ConfigurationManager.ConnectionStrings["AdvWorks"].ConnectionString;
string dbcon = ConfigurationManager.ConnectionStrings["mfwamba"].ConnectionString;
SqlConnection con = new SqlConnection(dbcon);
con.Open();
SqlCommand cmd = new SqlCommand("Select imPhoto from Emp where Empid=@empid", con);
cmd.Parameters.AddWithValue("@empid", context.Request.QueryString["id"].ToString());
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((byte[])dr["imPhoto"]);
dr.Close();
con.Close();
}
else
{
context.Response.Write("No Image Found");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
///////////////////////////////////////////
showDetails.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowDetails.aspx.cs" Inherits="ShowDetails" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
///////////////////////////////////
showDetails.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ShowDetails : System.Web.UI.Page
{
string strcon = "Data Source=localhost\\sqlexpress;Integrated Security=true;Initial Catalog=Mfwamba";
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(strcon);
con.Open();
cmd = new SqlCommand("Select * from Emp where Empid=@id", con);
cmd.Parameters.AddWithValue("@id", DropDownList1.Text);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
Label1.Text = dr[1].ToString();
Label2.Text = dr[2].ToString();
Image1.ImageUrl = "~/Handler.ashx?id=" + DropDownList1.Text;
}
con.Close();
}
}
/////////////////////////////////////////////////
web.config
Thanx,
Israel.

Iftikar HussainRank #423 · 5.9K reputationPosted Aug 14, 2013, 2:20 AM EDT
Put the break point in below line and check weather is it executing the line or not
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
Regards,
Iftikar
IsraelRank #1,223 · 1.5K reputationPosted Aug 14, 2013, 3:14 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 14, 2013, 3:17 AM EDT
Regards,
Iftikar
Khargesh RajputRank #616 · 3.9K reputationPosted Aug 14, 2013, 3:53 AM EDT
IsraelRank #1,223 · 1.5K reputationPosted Aug 14, 2013, 11:15 PM EDT
Abhineet SrivastavaRank #1,890 · 783 reputationPosted Aug 15, 2013, 1:35 AM EDT
IsraelRank #1,223 · 1.5K reputationPosted Aug 15, 2013, 2:37 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 15, 2013, 5:49 AM EDT
Regards,
Iftikar
IsraelRank #1,223 · 1.5K reputationPosted Aug 15, 2013, 6:20 AM EDT
Thanx.
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 15, 2013, 6:41 AM EDT