Click here to Skip to main content
15,897,163 members
Please Sign up or sign in to vote.
2.91/5 (3 votes)
See more:
i want insert pic in DB with procedure and 3-tier butt error in UI-tier! i have in my project a procedure , data access layer and business logic layer ... in Btn_Insert code , input values send to params ... then send to SP !

this Store Procedure :

C#
ALTER PROCEDURE [dbo].[Agent_Insert]
    @Agent NVARCHAR(MAX), 
    @Mob NVARCHAR(MAX),
    @Pic IMAGE, 
    @Toz NVARCHAR(MAX)
    AS 
    BEGIN 
    INSERT INTO Agent(Agent, Mob, Pic, Toz) 
    VALUES (@Agent, @Mob, @Pic, @Toz) 
    END


this DAL code :

C#
public int Agent_Insert(string Agent, string Mob, byte Pic, string Toz)
    {
        var returnValue = 0;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand dCmd = new SqlCommand("Agent_Insert", conn))
            {
                dCmd.CommandType = CommandType.StoredProcedure;
                SqlParameter[] prms = new SqlParameter[4];
                prms[0] = new SqlParameter("@Agent", SqlDbType.NVarChar);
                prms[0].Value = Agent;
                prms[1] = new SqlParameter("@Mob", SqlDbType.NVarChar);
                prms[1].Value = Mob;
                prms[2] = new SqlParameter("@Pic", SqlDbType.Image);
                prms[2].Value = Pic;
                prms[3] = new SqlParameter("@Toz", SqlDbType.NVarChar);
                prms[3].Value = Toz;
                dCmd.Parameters.AddRange(prms);
                conn.Open();
                returnValue = dCmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        return returnValue;
    }


this BLL code :

SQL
public class Agent_Bll
{
    public int Insert(string Agent, string Mob, byte Pic, string Toz)
    {
        return new Agent_Dal().Agent_Insert(Agent, Mob, Pic, Toz);
    }

    public int Update(int Id, string Agent, string Mob, byte Pic, string Toz)
    {
        return new Agent_Dal().Agent_Update(Id, Agent, Mob, Pic, Toz);
    }

    public int Delete(int Id)
    {
        return new Agent_Dal().Agent_Delete(Id);
    }

    public DataTable Select()
    {
        return new Agent_Dal().Agent_Select();
    }
}


and this code : (Btn_Insert)

C#
public static byte[] ImageToByte(Image img)
    {
        ImageConverter converter = new ImageConverter();
        return (byte[])converter.ConvertTo(img, typeof(byte[]));
    }

    private void Btn_Insert_Click(object sender, EventArgs e)
    {
       var result = 0;
       try
       {
           result = new Agent_Bll().Insert(Txt_Agent.Text.Trim(), Txt_Mob.Text.Trim(), pictureBox1.Image(ImageToByte), Txt_Toz.Text.Trim());


please help !
Posted
Comments
CHill60 23-Apr-15 19:17pm    
And the error is ...??

change pictureBox1.Image(ImageToByte) to ImageToByte(pictureBox1.Image)
 
Share this answer
 
thank's
This time I tried
But the error is : this pic
http://s4.picofile.com/file/8184508092/ImageToByte.png[^]
 
Share this answer
 
Comments
ghasem110deh 25-Apr-15 5:36am    
help me ...
guy's
MMA Defacer 25-Apr-15 6:04am    
HiConsider the DAL & BLL, your image is one byte! ImageToByte(pictureBox1.Image) returns an array of bytes and you should use byte[] instead of byte...
ghasem110deh 25-Apr-15 16:00pm    
thanks ...
is OK :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900