While i have try to insert values, i have getting mention error and i also unable to insert value from textarea.
Server Error in '/' Application.
Cannot call methods on nvarchar.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Cannot call methods on nvarchar. Source Error:
Line 54: // cmd.Parameters.Add("@str", SqlDbType.VarChar).Value = str.Text.ToString();
Line 55: cmd.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray;
Line 56: cmd.ExecuteNonQuery();
Line 57: BindGrid();
|
private void SaveImage()
{
if (FileUpload1.HasFile)
{
int imagefilelenth = FileUpload1.PostedFile.ContentLength;
byte[] imgarray = new byte[imagefilelenth];
HttpPostedFile image = FileUpload1.PostedFile;
image.InputStream.Read(imgarray, 0, imagefilelenth);
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=OCMS;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
SqlCommand cmd = new SqlCommand("Insert into IT_comp_box (EmpCode, COMP_MESSAGE,Image) values (@lblName.text, @str,@Image)", con);
con.Open();
cmd.Parameters.AddWithValue("@lblName", SqlDbType.VarChar).Value = lblName.Text;
// cmd.Parameters.AddWithValue("@str", SqlDbType.varchar).Value = str.Text.ToString();
cmd.Parameters.Add("@str", SqlDbType.VarChar).Value = str.Text.ToString();
cmd.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray;
cmd.ExecuteNonQuery();
BindGrid();
}
}
public void BindGrid()
{
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=OCMS;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
SqlCommand cmd = new SqlCommand("Select COMP_ID,EmpCode,COMP_MESSAGE,COMP_LODGE_DATE,CLOSING_DATE from IT_comp_box", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}