hi in my project am trying to save image into sqlserver and display it into grid..but while runtime am getting erorr in this code and record not saving to database?
//read image file
byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
try
{
byte[] imageData = (byte[])dataGridView1.Rows[e.RowIndex].Cells["cdCustomerPhoto"].Value;
Image newImage;
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);
newImage = Image.FromStream(ms, true);
}
pictureBox1.Image = newImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//btnsave clikc
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] imageData = ReadFile(txtImagePath.Text);
string Name = tbName.Text;
string Mobile = tbMobile.Text;
string Age = tbAge.Text;
string JoindDate = tbJoinDate.Text;
string Sex = tbSex.Text;
string DOB = tbDateOfBirth.Text;
string IDProof = tbIDProof.Text;
SqlConnection CN = new SqlConnection(@"Data Source=ythisbug;Initial Catalog=Exchange;Integrated Security=True;");
string qry = "insert into tblCustomerDetails (cdCustomerName,cdCustomerSex,cdCustomerPhNo,cdCustomerDOB,cdCustomerJoinDate,cdCustomerAge,cdCustomerIDProofNo,cdCustomerPhoto) values(@Name,@Sex,@Mobile,@Dob,@Join,@Age,@Idproof,@ImageData)";
SqlCommand SqlCom = new SqlCommand(qry, CN);
SqlCom.Parameters.Add(new SqlParameter("@Name", (object)tbName.Text));
SqlCom.Parameters.Add(new SqlParameter("@Sex", (object)tbSex.Text));
SqlCom.Parameters.Add(new SqlParameter("@Mobile", (object)tbMobile.Text));
SqlCom.Parameters.Add(new SqlParameter("@Dob", (object)tbDateOfBirth.Text));
SqlCom.Parameters.Add(new SqlParameter("@Join", (object)tbJoinDate.Text));
SqlCom.Parameters.Add(new SqlParameter("@Age", (object)tbAge.Text));
SqlCom.Parameters.Add(new SqlParameter("@Idproof", (object)tbIDProof.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
GetImagesFromDatabase();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//this errorr in btn click save
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Exchange.CustomerRegistration.button1_Click(Object sender, EventArgs e) in C:\Users\ythisbug\Documents\Visual Studio 2008\Projects\ShiroorExchangeWindowsApp\Exchange\CustomerRegistration.cs:line 75
//
here is my code
void GetImagesFromDatabase()
{
try
{
SqlConnection CN = new SqlConnection(@"Data Source=ythisBut;Initial Catalog=Exchange;Integrated Security=True;");
SqlDataAdapter ADAP = new SqlDataAdapter("Select * from tblCustomerDetails", CN);
DataSet DS = new DataSet();
ADAP.Fill(DS, "tblCustomerDetails");
dataGridView1.DataSource = DS.Tables["tblCustomerDetails"];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
error:
System invalid operation exception instance failure
at System.data.Common.dbdataadapter.fill(dataset dataset,string src table)
at Exchange.customerregistration.getImagesFromDatabase() in c:\Users\ythisbug\documents\visual studio 2008\projects\exchange\exchange\CustomerRegistration.cs:line 95
can any one help me to solve this..
thanks in advance