Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while i try this code in jquery tab control it shows me error that "Object reference not set to an instance of an object." i also tested in sql but here it can successfully inserted to the database

C#
protected void Button5_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                byte[] imagesize = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile uploadedimage = FileUpload1.PostedFile;
                uploadedimage.InputStream.Read(imagesize, 0, (int)FileUpload1.PostedFile.ContentLength);
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into room_info(room_image,room_type,info,conditions,single_price,double_price,sno) values(@room_image,'" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "' ,'" + TextBox14.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "')", con);
                SqlParameter Uploadedimage = new SqlParameter("@room_image", SqlDbType.Image, imagesize.Length);
                Uploadedimage.Value = imagesize;
                cmd.Parameters.Add(Uploadedimage);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        catch
        {
        }
    }
Posted
Updated 18-Jun-12 19:06pm
v2

Hi RempoRaaj,


I suggest you to try this alternative:

C#
protected void Button5_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName != "")
            {
                byte[] imagesize = FileUpload1.FileBytes;                
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into room_info (room_image,room_type,info,conditions,single_price,double_price, sno)  values(@room_image,'" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "' ,'" + TextBox14.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "')", con);
                SqlParameter Uploadedimage = new SqlParameter("@room_image", SqlDbType.Image, imagesize.Length);
                Uploadedimage.Value = imagesize;
                cmd.Parameters.Add(Uploadedimage);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        catch
        {
        }
    }



Hope this helps you.

Happy Coding ;)
Sunny_K
 
Share this answer
 
Comments
Sunny_Kumar_ 19-Jun-12 2:05am    
thanks for accepting this as answer.
CodeDZStar 19-Jun-12 6:04am    
Good WOrk +5!
Hi this may help you...



C#
if (fileupload1.HasFile)
       {
          byte [] productImage = fileupload1.FileBytes;

          con.Open();
           SqlCommand cmd = new SqlCommand("SP_SetUserDetails", con);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@Username", txtuname.Text);
           cmd.Parameters.AddWithValue("@EmailID", txtEmail.Text);
           cmd.Parameters.AddWithValue("@Password", txtpass.Text);
           cmd.Parameters.AddWithValue("@Image", productImage);
           cmd.Parameters.AddWithValue("@Hobbies", str);
           cmd.Parameters.AddWithValue("@country", txtcountry.Text);
           cmd.Parameters.AddWithValue("@Status", sts.ToString());
           //cmd.ExecuteNonQuery();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           Gridview1.DataSource = ds;
           //Gridview1.DataBind();
           BindGrid();
           //con.Close();

       }



Stored Procedure


SQL
<pre>ALTER procedure [dbo].[SP_SetUserDetails]
(
@Username varchar(50),
@EmailID varchar(50),
@Password varchar(50),
@Image varbinary(max),
@Hobbies varchar(max),
@country varchar(50),
@Status varchar(50)
)
as
begin
Insert into UserDetails(UserName,EmailId,Password,PImage,Hobbies,Country,Status)values(@Username,@EmailID,@Password,@Image,@Hobbies,@country,@Status)
end
 
Share this answer
 
Comments
CodeDZStar 19-Jun-12 6:04am    
Good work +5!

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