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];
Related
I tried the code below for displaying results in data list. When user logs in, I tried to pull the data according to their id, but the details do not display, here is my code:
string connn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(connn);
con.Open();
string str = "select details,address
from tb_userdata
inner join tb_userlogin
on tb_userdata.uidfromtb1=tb_userlogin.id";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Can any one tell me what is the problem with this code?
Your SQL query is missing WHERE statement. What you have in your query should show details for all users and not only for that specific user.
Try something like this and just update the part where parameter value is added
string connn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(connn);
con.Open();
string str = "select details,address from tb_userdata inner join tb_userlogin on tb_userdata.uid=tb_userlogin.id WHERE tb_userlogin.uid = #UID";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add(new SqlParameter("#UID", "retrieve UID somehow");
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
con.Dispose();
I have created 2 DropdownList and 1 GridView
1st DropdownList Loads and Displays DataBase Names Dynamically
2nd DropdownList Loads and Displays Table Names, Based on Database name Which selected in 1st drop down list
Based on Table Name Data has to be displayed in GridView........
I have written code which displays Database Names and working fine
private void populateDatabasename() {
SqlConnection con = new SqlConnection(#"Data Source=SAI- PC\SQLEXPRESS;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select name,collation_name from sys.databases order by name", con);
DataSet ds = new DataSet();
da.Fill(ds, "dbname");
DropDownList1.DataSource = ds.Tables["dbname"];
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
}
Based on Database name tables have to be displayed..... how to pass Database name(Which is selected in 1st drop down list) in the following code.....
Is This a correct way to pass database name
private void populateTableName() {
SqlConnection con = new SqlConnection(#"Data Source=SAI-PC\SQLEXPRESS;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select name from "+"#Dbname"+".sys.tables", con);
da.SelectCommand.Parameters.Add("#dbname", SqlDbType.VarChar);
da.SelectCommand.Parameters["#dbname"].Value = DropDownList1.SelectedValue;
DataSet ds = new DataSet();
da.Fill(ds, "dbname1");
DropDownList2.DataSource = ds.Tables["dbname1"];
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataBind();
}
I am not sure if it required or not but you should give the db name in connection string. Database=Northwind; in this case, using System.Data.SqlClient.SqlConnectionStringBuilder class even better. And then you can query the table names if the user have correct rights.
i think you should include DataBase name in connection string that you use to populate table
use something like this
SqlConnection con = new SqlConnection(#"Data Source=SAI-PC\SQLEXPRESS;Database=" + Dropdownlist1.selecteditem.text + ";Integrated Security=True");
then Read the tables and continue
Try this:
string dbName = ddlDbName.SelectedValue;
string strcon = "server=SAI-PC\\SQLEXPRESS;database= " + dbName + ";providerName=\"System.Data.SqlClient\"";
SqlConnection con = new SqlConnection(strcon);
con.Open();
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);
I am trying to bind same data reader object to 2 controls .one is gridview and second is formview.The gridview is getting bind but it formview didn't .Even i am closing the object after binding both.
Can anyone please let me know if it is possible or not.If yes then how?
This is my code:-
SqlConnection con = new SqlConnection(getconnectionstring());
SqlCommand cmd = new SqlCommand();
SqlCommand cmd1 = new SqlCommand();
//cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
cmd1.CommandText = "SELECT firstname FROM crudtable";
cmd.Connection = con;
cmd1.Connection = con;
con.Open();
SqlDataReader reader ;
SqlDataReader reader1;
reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
FormView1.DataSource = reader;
FormView1.DataBind();
reader.Close();
reader1 = cmd1.ExecuteReader();
ddl.DataSource = reader1;
ddl.DataTextField = "firstname";
ddl.DataBind();
Thanks in advance.
SqlDataReader is a forward only reader.
Provides a way of reading a forward-only stream of rows from a SQL
Server database.
Once the data is read, you can't go back to read it again.
That is why you can't use the same reader as a data source for another control without calling ExecuteReader again.
If the number of rows you get is small, you can fetch the data into a DataSet and bind that to both.
SqlConnection conn = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
FormView1.DataSource = ds;
FormView1.DataBind();
ddl.DataSource = ds;
ddl.DataTextField = "firstname";
ddl.DataBind();
Once you have the data in your DataSet, you can decide which columns to Bind and show.
For the most streamlined version of binding in this case I would modify the code like so:
DataTable results = new DataTable();
using (SqlConnection connection = new SqlConnection(getconnectionstring()))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT firstname,lastname FROM crudtable",connection))
{
results.Load(command.ExecuteReader());
}
}
GridView1.DataSource = results;
GridView1.DataBind();
FormView1.DataSource = results;
FormView1.DataBind();
ddl.DataSource = results;
ddl.DataTextField = "firstname";
ddl.DataBind();
If 2 controls are not binding to 1 data source then take that data source and bind it to table then copy that table
And its data to another table and bind them seperatley.
DataTable.clone() will fetch the structure.
DatTable.Copy() will fetch schema and records
Clone reference
Copy Reference
Finally i took a new reader object to bind formview,but still it didn't work.Am i wrong somewhere or missing something.
Getting no exception no error.
This is updated code.
SqlConnection con = new SqlConnection(getconnectionstring());
SqlCommand cmd = new SqlCommand();
SqlCommand cmd1 = new SqlCommand();
SqlCommand cmd2 = new SqlCommand();
//cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
cmd.CommandText = "SELECT firstname,lastname FROM crudtable";
cmd2.CommandText = "SELECT firstname,lastname FROM crudtable";
cmd1.CommandText = "SELECT firstname FROM crudtable";
cmd.Connection = con;
cmd1.Connection = con;
cmd2.Connection=con;
con.Open();
SqlDataReader reader ;
SqlDataReader reader1;
SqlDataReader reader2;
reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
//reader.Close();
reader2 = cmd2.ExecuteReader();
FormView1.DataSource = reader2;
FormView1.DataBind();
reader2.Close();
reader1 = cmd1.ExecuteReader();
ddl.DataSource = reader1;
ddl.DataTextField = "firstname";
ddl.DataBind();
reader1.Close();
I am using this code snippet to update values in my database :
SqlConnection con = new SqlConnection(#"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True");
string str = "sama#yahoo.com";
SqlCommand com2 = new SqlCommand("select [user_Account] from User in str where [user_Email]=sama#yahoo.com", con);
SqlCommand com = new SqlCommand("update User set [user_Account]=? WHERE [user_Email=#em]", con);
com.Parameters.AddWithValue("user_Account",str);
com.Parameters.AddWithValue("#em",str);
con.Open();
com.ExecuteNonQuery();
com2.ExecuteNonQuery();
con.Close();
but I get this error
Incorrect syntax near the keyword 'User'.
Line 40: com.ExecuteNonQuery();
"User" is a reserved word in SQL. Wrap the name of the table in square brackets to specify that it's the name of something:
[User]
Why are you using two separate SqlCommand objects?? Absolutely not needed..... I would try to either UPDATE or SELECT - don't mix two totally separate operations into a single call....
Also: you should use parametrized queries to avoid SQL injection attacks, and you should put your SqlConnection and SqlCommand objects into using blocks - try this:
string updateStmt =
"UPDATE dbo.[User] SET [user_Account] = #AccountValue WHERE [user_Email] = #UserEMail;";
using(SqlConnection con = new SqlConnection(#"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True"))
using(SqlCommand _cmd = new SqlCommand(updateStmt, con))
{
_cmd.Parameters.Add("#AccountValue", SqlDbType.VarChar, 100).Value = str;
_cmd.Parameters.Add("#UserEMail", SqlDbType.VarChar, 100).Value = str;
con.Open();
_cmd.ExecuteNonQuery();
con.Close();
}