Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlCommand cmd = new SqlCommand("INSERT INTO CompRecd(ComNo, ComCat, ComSubCat, Whom, City, MobNo, PhoNo, EmailID, FileData, TimeStamp, officer, HouseNo, [from], CompSubCat2, Locality,FileDataType , Complaint , ipaddress) VALUES(@ComNo, @ComCat, @ComSubCat, @Whom, @City, @MobNo, @PhoNo, @EmailID, @FileData, getdate(),@officer,@HouseNo, @from, @CompSubCat2, @Locality, @FileDataType,@Complaint, @ipaddress)", cnn);
// Trans.Commit();
// SqlCommand cmd = new SqlCommand("INSERT INTO CompRecd(ComNo, ComCat, ComSubCat, Whom, City, MobNo, PhoNo, EmailID, FileData,TimeStamp, officer, HouseNo, [from], CompSubCat2, Locality ,FileDataType, Complaint , ipaddress) VALUES('" + compid + "','" + ds.Tables[0].Rows[0]["ComCat"].ToString() + "', '" + ds.Tables[0].Rows[0]["ComSubCat"].ToString() + "','" + ds.Tables[0].Rows[0]["Whom"].ToString() + "','" + ds.Tables[0].Rows[0]["City"].ToString() + "', '" + ds.Tables[0].Rows[0]["MobNo"].ToString() + "','" + ds.Tables[0].Rows[0]["PhoNo"].ToString() + "','" + ds.Tables[0].Rows[0]["EmailID"].ToString() + "','" + ds.Tables[0].Rows[0]["FileData"].ToString() + "', getdate(),'" + item.Value + "','" + ds.Tables[0].Rows[0]["HouseNo"].ToString() + "', '" + ds.Tables[0].Rows[0]["from"].ToString() + "','" + ds.Tables[0].Rows[0]["CompSubCat2"].ToString() + "','" + ds.Tables[0].Rows[0]["Locality"].ToString() + "','" + ds.Tables[0].Rows[0]["FileDataType"].ToString() + "','" + ds.Tables[0].Rows[0]["Complaint"].ToString() + "','" + ds.Tables[0].Rows[0]["ipaddress"].ToString() + "')", cnn);
 cmd.Transaction = Trans;


 cmd.Parameters.Add("@ComNo", SqlDbType.VarChar).Value = compid;
 cmd.Parameters.Add("@ComCat", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["ComCat"].ToString();
 cmd.Parameters.Add("@ComSubCat", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["ComSubCat"].ToString();
 cmd.Parameters.Add("@Whom", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["Whom"].ToString();
 cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["City"].ToString();
 cmd.Parameters.Add("@MobNo", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["MobNo"].ToString();
 cmd.Parameters.Add("@PhoNo", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["PhoNo"].ToString();
 cmd.Parameters.Add("@EmailID", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["EmailID"].ToString();
 cmd.Parameters.Add("@officer", SqlDbType.Int).Value = item.Value;
 cmd.Parameters.Add("@HouseNo", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["HouseNo"].ToString();
 cmd.Parameters.Add("@from", SqlDbType.Int).Value = ds.Tables[0].Rows[0]["from"].ToString();
 cmd.Parameters.Add("@CompSubCat2", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["CompSubCat2"].ToString();
 cmd.Parameters.Add("@Locality", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["Locality"].ToString();
 cmd.Parameters.Add("@FileData", SqlDbType.VarBinary).Value = ds.Tables[0].Rows[0]["FileData"].ToString();
 cmd.Parameters.Add("@FileDataType", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["FileDataType"].ToString();
 cmd.Parameters.Add("@Complaint", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["Complaint"].ToString();
 cmd.Parameters.Add("@ipaddress", SqlDbType.VarChar).Value = ds.Tables[0].Rows[0]["ipaddress"].ToString();
 cmd.ExecuteNonQuery();


What I have tried:

i want to store filedata(string byte[]) into database
filedata(column name) varbinary(max) datatype into my table
Posted
Updated 2-Aug-16 21:13pm
v3

1 solution

You are assigning string data to the parameter instead of binary data.
Try following-
C#
cmd.Parameters.Add("@FileData", SqlDbType.VarBinary).Value = (byte[]) ds.Tables[0].Rows[0]["FileData"];


Hope, it helps :)
 
Share this answer
 
Comments
Nishant.Chauhan80 3-Aug-16 3:14am    
Thanks its work this code
Suvendu Shekhar Giri 3-Aug-16 3:19am    
Glad that it helped :)

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