Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai below is my code

C#
String conecion = "provider=Microsoft.ACE.OLEDB.12.0;" + @"data source=d:\DataXL.xlsx;" + "Extended Properties='Excel 12.0 Xml;HDR=YES'";
        OleDbConnection con = new OleDbConnection(conecion);
       
 string sql = "INSERT INTO [Sheet1$] ([Name], [EmailId], [Phone no], [Address], [Height], [Weight], [Program], [Plan], [AmountPaid], [DueAmount])  VALUES(@nam,@emlid,@phonno,@addrs,@higt,@wigt,@selprog,@selplan,@amp,@amtdue)";
 OleDbCommand cmdd = new OleDbCommand(sql);
cmdd.Parameters.AddWithValue("@nam", nametxt.Text);
            cmdd.Parameters.AddWithValue("@emlid", emailtxt.Text);
            cmdd.Parameters.AddWithValue("@phonno", phno.Text);
            cmdd.Parameters.AddWithValue("@addrs", addresstxt.Text);
            cmdd.Parameters.AddWithValue("@higt", heighttxt.Text);
            cmdd.Parameters.AddWithValue("@wigt", weighttxt.Text);
            cmdd.Parameters.AddWithValue("@selprog", programtxt.SelectedItem.Text);
            cmdd.Parameters.AddWithValue("@selplan", DropDownList2.SelectedItem.Text);
            cmdd.Parameters.AddWithValue("@amp", paidamountxt.Text);
            cmdd.Parameters.AddWithValue("@amtdue", amountdue.Text);
            con.Open();
            cmdd.ExecuteNonQuery();
con.Close();

Error i am getting is give below:
connection property not initialized


Kindly suggest me.

Thankyou.
Posted
Updated 1-Mar-15 21:39pm
v2

1 solution

because you have not initialized the connection for the command.

Instead of this...
C#
OleDbCommand cmdd = new OleDbCommand(sql);

Do this...
C#
OleDbCommand cmdd = new OleDbCommand(sql, con);
 
Share this answer
 
v3
Comments
kalyan10qwerty 2-Mar-15 3:45am    
I have already done that then it is saying
The best overload method match for 'System.Data.OleDb.OleDbCommand(string, System.Data.OleDb.OleDbConnection) has some invalid arguments

So plz give another solution
I did have a mistake. Now corrected. The spelling was wrong. Your variable is "con". I updated. Have you tried with this only?
Yes, it should be...

OleDbCommand cmdd = new OleDbCommand(sql, con);

My bad. try now.

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