Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello
In my project data was not inserting.It shows an error that "connection property has not been initialized executenonquery".How to solve this?

Code1 --- Save Image
C#
private void btn_SaveImage_Click(object sender, EventArgs e)
       {

           try
           {
               string connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
               Image_SqlCo = new SqlConnection(connectionstring);
               this.Image_SqlCo.Open();
               if (Image_sqlcmd.Parameters.Count == 0)
               {
                   this.Image_sqlcmd.CommandText = "INSERT INTO tbl_ImageData(IID, Name,Picture) values(@ID,@Name,@Picture)";
                   this.Image_sqlcmd.Parameters.Add("@ID", System.Data.SqlDbType.Int, 4);
                   this.Image_sqlcmd.Parameters.Add("@Name", System.Data.SqlDbType.VarChar, 50);
                   this.Image_sqlcmd.Parameters.Add("@Picture", System.Data.SqlDbType.Image);

               }
               this.Image_sqlcmd.Parameters["@ID"].Value = this.txtId.Text;
               this.Image_sqlcmd.Parameters["@Name"].Value = this.txtImagePath.Text;
               this.Image_sqlcmd.Parameters["@Picture"].Value = this.m_barrImg;

               int iResult = this.Image_sqlcmd.ExecuteNonQuery();
               MessageBox.Show(Convert.ToString(iResult));
           }
           catch (Exception ex1)
           {
               MessageBox.Show(ex1.ToString());
           }

           finally
           {
               this.Image_SqlCo.Close();
           }

       }



Code2--- AppConfig

XML
<<blockquote class="FQ"><div class="FQA">Quote:</div>?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>

    <add name="Constr" connectionString="Server=.\SQLEXPRESS; Integrated Security=true;Initial Catalog=IMS"/>
  </connectionStrings>
</configuration</blockquote>>
Posted

1 solution

You forgot to add the Connection to the Command. Try this:
C#
Image_sqlcmd.Connection = Image_SqlC;
After the Open.
 
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