Click here to Skip to main content
15,913,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I got the following error while insert value in the database


Failed to convert parameter value from a SqlParameter to a String.


C#
cmd = new SqlCommand("insert into user_senditem(user_senditem_id,username,user_id,department_id,attached_files,subject,message,compl_nature,compl_type,group_id,apartment_id,area_id,date_time,type,created_on,created_by,Image) values(@usendid,@username,@userid,@deptid,@attached,@subject,@message,@cmplnature,@comptype,@groupid,@apartmentid,@areaid,@datetime,@type,@createdon,@createdby,@image2)", conn);
                        

                    SqlParameter usendid = new SqlParameter("@usendid", SqlDbType.VarChar, 50);
                      usendid.Value = txtUserSentId.Text;                    
                      cmd.Parameters.Add(usendid);

                  SqlParameter username = new SqlParameter("@username ", SqlDbType.VarChar, 50);
                    username.Value = Session["username"];
                    cmd.Parameters.Add(username);


                    SqlParameter userid = new SqlParameter("@userid", SqlDbType.VarChar, 50);
                   userid.Value = Session["id"];
                    cmd.Parameters.Add(userid);


                    SqlParameter deptid = new SqlParameter("@deptid", SqlDbType.VarChar, 50);
                    deptid.Value = "Admin";
                    cmd.Parameters.Add(deptid);

                   SqlParameter attached = new SqlParameter("@attached", SqlDbType.VarChar, 50);
                    attached.Value = uploadedDocument;
                    cmd.Parameters.Add(attached);

                    SqlParameter subject = new SqlParameter("@subject", SqlDbType.VarChar, 50);
                    subject.Value = txtSubject.Text;
                    cmd.Parameters.Add(subject);


                    SqlParameter message = new SqlParameter("@message", SqlDbType.VarChar, 300);
                    message.Value = txtMessage.Text;
                    cmd.Parameters.Add(message);
                        

               SqlParameter cmplnature = new SqlParameter("@cmplnature", SqlDbType.VarChar, 50);
                    cmplnature.Value = "Complaint";
                    cmd.Parameters.Add(cmplnature);

                 SqlParameter comptype = new SqlParameter("@comptype", SqlDbType.VarChar, 50);
                    comptype.Value = ddl_ComplType.SelectedItem;
                    cmd.Parameters.Add(comptype);


                    SqlParameter groupid = new SqlParameter("@groupid", SqlDbType.VarChar, 50);
                    groupid.Value = Session["group_id"];
                    cmd.Parameters.Add(groupid);

             SqlParameter apartmentid = new SqlParameter("@apartmentid", SqlDbType.VarChar, 50);
                    apartmentid.Value = Session["apartment_id"];
                    cmd.Parameters.Add(apartmentid);


                    SqlParameter areaid = new SqlParameter("@areaid", SqlDbType.VarChar, 50);
                    areaid.Value = Session["area_id"];
                    cmd.Parameters.Add(areaid);

                    SqlParameter datetime = new SqlParameter("@datetime", SqlDbType.DateTime);
                    datetime.Value = lblDatetime.Text;
                    cmd.Parameters.Add(datetime);


                    SqlParameter type = new SqlParameter("@type", SqlDbType.VarChar, 50);
                    type.Value = "N";
                    cmd.Parameters.Add(type);

                SqlParameter createdon = new SqlParameter("@createdon", SqlDbType.VarChar, 50);
                    createdon.Value = DateTime.Now;
                    cmd.Parameters.Add(createdon);



                 SqlParameter createdby = new SqlParameter("@createdby", SqlDbType.VarChar, 50);
                    createdby.Value = SystemInformation.UserName;
                    cmd.Parameters.Add(createdby);

                  SqlParameter image2 = new SqlParameter("@image2", SqlDbType.Binary, filelen);
                    image2.Value = buffer;
                    cmd.Parameters.Add(image2);  

                        
                        cmd.ExecuteNonQuery();
Posted
Updated 20-Dec-16 13:49pm
v2

You didn't tell us which SQLParameter caused the error. I did notice, though, that you did not convert the date to DateTime format before putting it into the SQLParameter.


SqlParameter datetime = new SqlParameter("@datetime", SqlDbType.DateTime);
datetime.Value = DateTime.Parse(lblDatetime.Text);
cmd.Parameters.Add(datetime);


Note: My example does no error checking. It is possible that the user enters a date in an incorrect format. You should have error checking to prevent that.


I also found where you were assigning a DateTime type value to a VarChar type. I corrected that by changing the parameter type to DateTime.
SqlParameter createdon = new SqlParameter("@createdon", SqlDbType.DateTime, 50);
    createdon.Value = DateTime.Now;
    cmd.Parameters.Add(createdon);
 
Share this answer
 
v2
Comments
Deenuji 29-Aug-13 0:36am    
i make change as per ur solution still getting same error
Mike Meinz 29-Aug-13 7:47am    
Without more details, it is not possible to further debug your program for you. You have one or more SQLParameters that do not match their associated database declaration. You could use the Visual Studio interactive debugger to step through the code to determine which one. Also, you could use the Improve Question button to add the Create Table SQL DDL to your question so that we could compare each SQLParameter to the associated column's declaration for you.
Just a note...
Shorter way of doing this is just by saying

cmd.Parameters.AddWithValue("createdon", DateTime.Now);
 
Share this answer
 
Actually what is the problem means i have using uploaddocument in my coding...that document was store in binary format in db..tats wat getting this error...thanks guys..
 
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