Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void Button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.InitialDirectory = Directory.GetCurrentDirectory();
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                FileInfo fi = new FileInfo(openFileDlg.FileName);
                FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
                BinaryReader rdr = new BinaryReader(fs);
                byte[] fileData = rdr.ReadBytes((int)fs.Length);
                rdr.Close();
                fs.Close();

                string cs = @"Data            Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SATDB.mdf;Integrated Security=True;User Instance=True";
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    string sql = "INSERT INTO SAT VALUES (@Trial_Addmission, default)";
                    SqlCommand cmd = new SqlCommand(sql, con);
                    cmd.Parameters.Add("@Trial_Addmission", SqlDbType.VarBinary, fileData.Length).Value = fileData;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }
Posted
Updated 3-Sep-13 9:13am
v4
Comments
[no name] 3-Sep-13 15:02pm    
Okay I see the code dump but where is the question?
Karim2Ahmed 3-Sep-13 15:07pm    
what is the wrong with this code??
[no name] 3-Sep-13 15:17pm    
Can't you share an error? Or at least tell us what the problem is.
Mike Meinz 3-Sep-13 15:09pm    
The Insert contains two values (@Trial_Addmission, default). Does the database table SAT include only two values? If not, that is your error. When you do not name the table's columns in the insert statement, it is assumed that you will provide values for all of the columns.

Probably, there is a field other than the ones you are trying to insert that you haven't specified. But it may well be that your are trying to set more columns than you actually have...

It may be an ID field, that you have set to Identity, or it may be a not-null field. Either way, you can't insert like that.
Always name your field when you INSERT:
C#
string sql = "INSERT INTO SAT (TrialColumn, DefaultColumn) VALUES (@Trial_Addmission, default)";
That will get rid of any problem with Identity fields, but you may need to check the datatype of your defaulted value, as I'm not sure the word "default" will work there! :laugh:
 
Share this answer
 
In your SQL INSERT statement you don't specify the field names only the values.

Replace ColumnName1 and ColumnName2 with column names in SQL where you are putting the @Trial_Addmission and default values.
C#
string sql = "INSERT INTO SAT (ColumnName1,ColumnName2) VALUES (@Trial_Addmission, default)";
 
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