it agains error occur microsoft jet database engine cannot open file it is already opened exclusively by another user or you need permissions to view its data
Cs
protected void btnUpload1_Click(object sender, EventArgs e)
{
// create some string variables and assign null values
string temp1 = "";
string temp2 = "";
string temp3 = "";
string temp4 = "";
string temp5 = "";
string temp6 = "";
string temp7 = "";
string temp8 = "";
//string myfile_name = Path.GetFileName(FileUpload1.PostedFile.FileName);
// assign session object data to myfile_name variable
string myfile_name = Path.GetFileName(FileUpload1.PostedFile.FileName);
// get complete path of excel sheet and assing it Excel_path variable
string Excel_path = Server.MapPath(myfile_name);
// create connection with excel database
OleDbConnection my_con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Excel_path + ";Extended Properties=Excel 8.0;Persist Security Info=False");
my_con.Open();
try
{
// get the excel file data and assign it in OleDbcoomad object(o_cmd)
OleDbCommand o_cmd = new OleDbCommand("select*from [Sheet1$]", my_con);
// read the excel file data and assing it o_dr object
OleDbDataReader o_dr = o_cmd.ExecuteReader();
while (o_dr.Read())
{
//get first row data and assign it ex_id variable
temp1 = o_dr[0].ToString();
//get second row data and assign it ex_name variable
temp2 = o_dr[1].ToString();
//get thirdt row data and assign it ex_name variable
temp3 = o_dr[2].ToString();
//get first row data and assign it ex_location variable
temp4 = o_dr[3].ToString();
temp5 = o_dr[4].ToString();
temp6 = o_dr[5].ToString();
temp7 = o_dr[6].ToString();
temp8 = o_dr[7].ToString();
// create a connection string with your sql database
SqlConnection con = new SqlConnection("server=SANAMDEEP;uid=sa;pwd=sa123;database=School");
con.Open();
//insert excel data in student table
SqlCommand cmd = new SqlCommand("insert into tbl_dept_desg values(@country,@University Type, @University Name, @Date of Contract, @Expiry Date, @Contact Person, @Contact No., @Email-Id)", con);
cmd.Parameters.AddWithValue("country", temp1);
cmd.Parameters.AddWithValue("University Type", temp2);
cmd.Parameters.AddWithValue("University Name", temp3);
cmd.Parameters.AddWithValue("Date of Contract", temp4);
cmd.Parameters.AddWithValue("Expiry Date", temp5);
cmd.Parameters.AddWithValue("Contact Person", temp6);
cmd.Parameters.AddWithValue("Contact No.", temp7);
cmd.Parameters.AddWithValue("Email-Id", temp8);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
// Label1.Text = "Data inserted successfully";
}
con.Close();
}
}
catch (Exception)
{
//Label1.Text = ex.Message;
}
}
Member
19 Points
84 Posts
Re: when data save from gridview to database an error occur object reference
Jun 19, 2020 06:48 AM|sanam13|LINK