Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
byte[] img = null;
                FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                img = br.ReadBytes((int)fs.Length);
                string sql = "INSERT INTO tbl_Details(Name,FName,Contact            no,Email,Department,BloodGroup,Age,Image) VALUES (" + txtname.Text + ",'" + txtFname.Text + "','" + txtcell.Text + "','" + txtemail.Text + "','" + txtdepartment.Text + "','" + txtbloodgroup.Text + "','" + txtage.Text + "',@img)";
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add(new SqlParameter("@img", img));
                int x = cmd.ExecuteNonQuery();
                MessageBox.Show(x.ToString() + "Record(s) Saved!");
                conn.Close();
Posted
Updated 15-Sep-13 8:14am
v2
Comments
CHill60 15-Sep-13 14:14pm    
And your question is?
Joan M 15-Sep-13 14:25pm    
Mohsin Azam, as CHill60 asked you, it is not clear what you need, well, somehow it is clear, but people here expect to see a Little effort from the person who is asking something, therefore, a good recommendation is to work your questions and give the máximum amount of detail posible.

Keep in mind that the Questions & Answers section of CP is a highway of questions that lots of CPians try to answer. In the mind of the one that is willing to answer a question that is a bunch of code only is disgusting: the one who asks is not putting effort in the question... why should I put effort in the answer?...

Improve that for your next questions and you will discover that Codeproject is a wonderful community.

PS: I'm mad at my IE10 for detecting what I'm writting as Spanish.. if there is something that has no sense... I'm sorry for that. ;)
Mohsin Azam 15-Sep-13 14:33pm    
Thanks Joan Murt bro!

1 solution

You haven't said what kind of database you're using but I will assume SQL given the error message. Name is a reserved word, and should not be used for column names. Try surrounding it with square brackets [Name] to get over this problem at the moment. See also list of sql reserved words[^]

Your construction of the string sql will leave you open to SQL Injection attacks - even if this is an internal or private piece of coding it is still a good idea to get into good habits ... see http://www.dotnetperls.com/sqlparameter[^]
 
Share this answer
 
Comments
Mohsin Azam 15-Sep-13 14:36pm    
No,When I enter "Name" or Contact No ,then it will give me Erroe.
Invalid Column Name....ALthough "Name" and "Contact No"are reserved words
CHill60 15-Sep-13 14:54pm    
"Name" is a reserved word, "Contact No" is (are?) not reserved word(s). Do you actually have columns with the names you have used on your database table?
Mohsin Azam 16-Sep-13 4:47am    
CREATE TABLE [dbo].[tbl_Details](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[FName] [varchar](50) NULL,
[Contact No] [nvarchar](50) NOT NULL,
[Email] [varchar](50) NULL,
[Department] [varchar](50) NOT NULL,
[Blood Group] [varchar](3) NOT NULL,
[Age] [int] NULL,
[Image] [image] NOT NULL,
CONSTRAINT [PK_tbl_Details] PRIMARY KEY CLUSTERED


This is SQL Query?
CHill60 16-Sep-13 12:10pm    
There is a space in [Blood Group] in your Create table sql but not your Insert sql "INSERT INTO tbl_Details(Name,FName,Contact no,Email,Department,BloodGroup,"

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