Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i created sp to insert textbox field and radfield and image field.But i stuck with code in aspx.cs i don't know how to call in page to insert image can any one help me?

please suggest me.
Posted
Updated 8-May-12 21:25pm
v2

C#
        SqlParameter parReturn;
        int i;
        Cmd = new SqlCommand();

        try
        {
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.CommandText = "your procedure name";
            Cmd.Parameters.Add(new SqlParameter("@textbox", Textbox1.Text));
            Cmd.Parameters.Add(new SqlParameter("@radiobutton", Radiobutton1.Vale));
            Cmd.Parameters.Add(new SqlParameter("@image ", Image1.Text));
            parReturn = Cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int));
            parReturn.Direction = ParameterDirection.ReturnValue;
//Make your connection here            
Cmd.Connection.Open();
            Cmd.ExecuteNonQuery();
            i = Convert.ToInt32(Cmd.Parameters["ReturnValue"].Value.ToString());
        }
        catch
        {
            i = 0;
        }
        finally
        {
            Cmd.Connection.Close();
        }
        return i;
    }


Try this..
Happy coding.
 
Share this answer
 
v2
Comments
ythisbug 9-May-12 1:37am    
wat about for image..in sp i gave @Image",sqldbtype.Image);
and wat about Radio button value
AshishChaudha 9-May-12 2:11am    
What you are saving in database (filename or in bytes) in case of image???

I had improved the solution.Check it out..
ythisbug 9-May-12 2:20am    
ya image and radio button and text field..thanks i wil try this..
ythisbug 9-May-12 2:25am    
hi can u tel me parReturn = Cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int)); this line means?for which value u assigned?
AshishChaudha 9-May-12 2:28am    
When there is successful execution of procedure it returns a integer value..for this you have to write return @@identity after your insert query in procedure.
hi...to upload image into db..use "Fileupload control"..
..Here is the sample code..
C#
if (FileUpload1.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUpload1.FileName);
                HttpPostedFile Image = FileUpload1.PostedFile;
                byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
                Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);


                cmd.Parameters.AddWithValue("@Logo", myimage);
                lblError.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {

                lblError.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }



..use this code to save image in db..
----cmd.Parameters.AddWithValue("@Logo", myimage);----
is the command..which add the image to db...
 
Share this answer
 
Comments
ythisbug 9-May-12 5:04am    
thanks
Hi,

Try this:
C#
SqlParameter parReturn;
        int i;
        try
        {
            con.Open();
            Cmd = new SqlCommand("spTest", con);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.Add(new SqlParameter("@textbox", Textbox1.Text));
            Cmd.Parameters.Add(new SqlParameter("@radiobutton", Radiobutton1.Value));
            Cmd.Parameters.Add(new SqlParameter("@image ", Image1.Text));
            parReturn = Cmd.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int));
            parReturn.Direction = ParameterDirection.Output;
            Cmd.ExecuteNonQuery();
            i = Convert.ToInt32(Cmd.Parameters["ReturnValue"].Value.ToString());
        }
        catch(Exception ex)
        {
            i = 0;
        }
        finally
        {
            con.Close();
        }
        return i;
    }

All the best.

-AK
 
Share this answer
 
Comments
ythisbug 9-May-12 5:04am    
thanks
i tried simple withot sp

  if (imageUpload.HasFile)
        {
            SqlConnection con = new SqlConnection("Data Source=accerlap2;Initial Catalog=Fahad;User ID=sa;Password=p@ssword;");
            byte[] img = new byte[imageUpload.PostedFile.ContentLength];
            HttpPostedFile myimg=imageUpload.PostedFile;
            myimg.InputStream.Read(img, 0, imageUpload.PostedFile.ContentLength);
            SqlCommand cmd=new SqlCommand("Insert into tblImage(iName,iAddress,iImage) values(@Name,@Address,@Image)",con);
           
            cmd.Connection = con;
            SqlParameter name = new SqlParameter("@Name", SqlDbType.VarChar, 50);
            name.Value = txtName.Text;
            cmd.Parameters.Add(name);
            SqlParameter Address = new SqlParameter("@Address", SqlDbType.VarChar, 50);
            Address.Value = txtAddress.Text;
            cmd.Parameters.Add(Address);
            SqlParameter image = new SqlParameter("@Image", SqlDbType.Image);
            image.Value = img;
            cmd.Parameters.Add(image);

            con.Open();
            cmd.ExecuteNonQuery();
           
            
           
            lblMessage.Text = "Save Successfully";
            con.Close();
            txtName.Text = "";
            txtAddress.Text = "";
        }
        else
            lblMessage.Text = "Can't upload.....";
}
 
Share this answer
 

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