Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button1_Click_1(object sender, EventArgs e)
     {

            try
            {
                string fname = txtfname.Text;
                string lname = txtlname.Text;
                string house = txthouse.Text;
                string city = txtcity.Text;
                string country = txtcountry.Text;
                string state = txtstate.Text;
                string post = txtpost.Text;
                string mail = txtmail.Text;
                string blood = txtblood.Text;
                string mobile = txtmobile.Text;
                string dob = txtdob.Text;
                if (label24.Text != "file path")
                {
                    byte[] imagedata = ReadFile(label24.Text);
                    string conn = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|NORTHWND.MDF;Integrated Security=True;User Instance=True";
                    SqlConnection con = new SqlConnection(conn);
                    string query = "insert into address(firstname,lastname,house,city,country,state,post,mail,blood,mobile,dob,image)values
('" + fname + "','" + lname + "','" + house + "','" + city + "','" + country + "','" + state + "','" + post + "','" + mail + "','" + blood + "','" + mobile + "','" + dob + "',@image)";
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.Parameters.Add(new SqlParameter("@image", (object)imagedata));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    rInsert.Text = "Address Saved Sucessfully...!!!";
                }
                else
                    MessageBox.Show("Please Insert a Photo", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }


        }

        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
Posted
Updated 9-Sep-12 21:57pm
v2
Comments
sahabiswarup 10-Sep-12 3:44am    
what error you are getting while saving this?
JeseemRazak619 10-Sep-12 3:59am    
No error. its showing successful but in database the data's are not stored
JeseemRazak619 10-Sep-12 4:09am    
private void button4_Click_1(object sender, EventArgs e)
{
string title = txtsearch.Text.ToString();
if (title != "")
{

string conn = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|NORTHWND.MDF;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conn);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT firstname,lastname,house,city,country,state,post,mail,blood,mobile,dob FROM address WHERE firstname LIKE'" + title + "%' OR lastname LIKE'" + title + "%'", con);
da.Fill(ds, "address");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "address";
con.Close();
}
else
{
MessageBox.Show("You muste enter name", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}


//in same program the above code works successfully.
Philip Stuyck 10-Sep-12 4:11am    
this totally unreadable.
Philip Stuyck 10-Sep-12 4:11am    
replace cmd.ExecuteNonQuery();
with int i = cmd.ExecuteNonQuery();
and display i in your text message. You don't have to go to the db manually to check if something is inserted.

1 solution

Check out this link :
http://geekswithblogs.net/dotNETvinz/archive/2009/04/23/uploading-and-storing-images-to-database-in-asp.net.aspx[^]

you will have to scroll down to the bottom to get to the meaningfull information.
Bottom line is that I see that your parameter definition is wrong:
param[0] = new SqlParameter("@img", SqlDbType.Image, length);
 
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