Why does it throws error like this? - asp.net

I do import the data from Excel sheet to Sql database..everything is fine,when i run this code,my access engine could not find mt sheet,it throws like this error..
my error is
The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
but i already checked with my designation folder,its correct..then i don't know why it's repeated
my C# code below..
public partial class _Default : System.Web.UI.Page
{
string constr = #"Data Source=VIS1-B12\SQLEXPRESS;Initial Catalog=Sql_Excel;Integrated Security=True providerName=System.Data.SqlClient" ;
protected void btn_okClick(object sender, EventArgs e)
{
string path = Fup_Excel.PostedFile.FileName;
string exconstr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties=Excel 8.0";
OleDbConnection excelcon = new OleDbConnection(exconstr);
excelcon.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", excelcon);
OleDbDataReader dbreader;
OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
//dap.Fill(ds,"sheet1");
dbreader = cmd.ExecuteReader();
SqlBulkCopy bcpy = new SqlBulkCopy(constr);
bcpy.DestinationTableName = "Excel_Details";
bcpy.WriteToServer(dbreader);
//GridView1.DataSource = ds.Tables[0].DefaultView;
//GridView1.DataBind();
excelcon.Close();
}
}

It's better you should retrieve excel sheet name using code.
Code is shown below
OleDbConnection con = new OleDbConnection(ConnString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);

Related

SelectedIndexChanged DataBind error when using header filter

I'm trying to filter my GridView with values from a header dropdown.
However I get this error message: "DataBinding: 'System.Data.DataRowView' does not contain a property with the name '[AnotherColumnThatIsNotSTATUS]'"
[AnotherColumnThatIsNotSTATUS] = another column for some reason and not STATUS?
Can anyone see what I´m missing?
protected void ddlStatusHeader_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlStatusHeader = ((DropDownList)sender);
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT [Status] FROM [BI_Planning].[dbo].[tblStatus] WHERE [Status] LIKE '%' + #Status + '%' ", con);
cmd.Parameters.AddWithValue("#Status", ddlStatusHeader.SelectedValue);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Status");
gwActivity.DataSource = ds;
gwActivity.DataBind();
}
}
}
This is working for me.Hope this helps..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select [text] from main where status='"+DropDownList1.SelectedValue.ToString()+"'";
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Status");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
Have you specified Columns for your Gridview??

Not able to access .DBF file in asp.net

I am not able read .dbf file in asp.net.
Error : External table is not in the expected format.
1) Using OLEDB
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\VisualFoxproDB;Extended Properties=dBASE 5.0;");
OleDbCommand command = new OleDbCommand("SELECT ID,NAME FROM table.dbf", conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
gv1.DataSource = dt;
gv1.DataBind();
conn.Close();
.
2. Using ODBC
OdbcConnection conn = new OdbcConnection("Driver={Driver do Microsoft dBase (*.dbf)};DriverId=277;SourceType=DBF;SourceDB=D:\\VisualFoxproDB\\;Exclusive=No");
conn.Open();
string strQuery = "SELECT * FROM D:\\VisualFoxproDB\\test.dbf";
System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];

Unable to do search for selected items

I am using this code to search for selected items inside a checkbox list and it doesn't work.
protected void btnSearchCode_Click(object sender, ImageClickEventArgs e)
{
string selectedValues = string.Empty;
foreach (ListItem item in cblCode.Items)
{
if (item.Selected)
selectedValues += item.Value + ",";
}
if (selectedValues != string.Empty)
selectedValues = selectedValues.Remove(selectedValues.Length - 1);
cblCode.DataSource = DataReport.SearchCode(selectedValues);
cblCode.DataBind();
}
public static DataTable SearchCode(string selectedValues)
{
string strcon = ConfigurationManager.ConnectionStrings["LocalDB"].ConnectionString;
DataTable datatable = new DataTable();
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
SqlCommand command = new SqlCommand();
string strQuery = "Select Group, Name from Details where Code in (" + selectedValues + ")", conn;
command.Connection = conn;
SqlDataAdapter dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = command;
DataSet ds = new DataSet();
dataadapter.Fill(datatable);
}
return datatable;
}
Really appreciate any help on this.
You have not used strQuery at all.
Try this :
public static DataTable SearchCode(string selectedValues)
{
string strcon = ConfigurationManager.ConnectionStrings["LocalDB"].ConnectionString;
DataTable datatable = new DataTable();
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
string strQuery = "Select Group, Name from Details where Code in (" + selectedValues + ")";
SqlCommand command = new SqlCommand(strQuery, conn);
SqlDataAdapter dataadapter = new SqlDataAdapter();
dataadapter.SelectCommand = command;
DataSet ds = new DataSet();
dataadapter.Fill(datatable);
}
return datatable;
}
You create the query string but you never assign it to the command variable. Therefore, when you assign it to selectCommand, there isn't anything to query from DB. You want to add this line of code to assign the query string to variable:
command = new SqlCommand(strQuery,conn);
Always Use SqlParamerter while passing parameters to the database
i think You are missing single inverted comma because Your SeletedValues is string
string strQuery = "Select Group, Name from Details
where Code in ('" + selectedValues + "')", conn;

Data list where clause

i am using a datalist to display videos but i am trying to get it working now with the where clasue ...where the name is equal to wrd.mp4 i am getting the following error,
$exception {"The multi-part identifier \"wrd.mp4\" could not be bound."} System.Exception {System.Data.SqlClient.SqlException}
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Id, Name from tblFiles where Name=wrd.mp4";
cmd.Connection = con;
con.Open();
DataList1.DataSource = cmd.ExecuteReader();
DataList1.DataBind();
con.Close();
}
}
}
}
You need to use quotes:
cmd.CommandText = "select Id, Name from tblFiles where Name='wrd.mp4'";

searching in populated GridView using Select statements

I have a gridview on my webpage that binds from an excel sheet using OleDb
Now I'm trying to set it up so that people can search just by typing a word in the textbox, and then after button click, it'd filter all the results that have the same word in any column
I was advised to use this code:
protected void Button1_Click(object sender, EventArgs e)
{
string search = TextBox1.Text;
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
grvCarProof.DataSource = dt.Select("CP number = ");
grvCarProof.DataBind();
}
what I can't figure out is what kind of writing should go in my SelectCommand(). Am i on the right track
Use DataView and its RowFilter property.
DataView dv=dt.DefaultView;
dv.RowFilter="[Cp Number]='value']";
grvCarProof.DataSource = dv;
grvCarProof.DataBind();
Or add WHERE clause,
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$] where [column1]=#col1", conn);
cmd.Parameters.AddWithValue("#col1",inputValueFromUser);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
grvCarProof.DataSource = dt;
grvCarProof.DataBind();

Resources