tags:

views:

633

answers:

3

Hey all,

Have an Excel data source which I am querying ok and posting data in data grid. The trick is that I want the data in a string variable but am failing to do that. I have tried arrayLists and arrays and I am not getting there yet because dataset has multiple data types (numbers, strings).

Please help me. Source code:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data.OleDb" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System" %>

<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
string strConn;

    string str="08/PST";
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=|DataDirectory|marks.xls;" +
"Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT [Subject ID] FROM [Employees$] where emp_id='" + str + "'", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
DataGrid1.DataBind();
}
</script>
<html>
<head><title>Data Exported</title></head>
<body style="FONT-FAMILY: arial">
    <h2>Simple Data Report 
    </h2>
    <hr size="1" />
    <form id="Form1" runat="server">
        <asp:datagrid id="DataGrid1" runat="server" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>

Thanks in advance

+2  A: 

Right now your dataset is only getting the [Subject Id] in your dataset. So would you consider changing the query itself to return a string?

SQL SERVER:

SELECT CONVERT (VarChar, [Subject ID]) FROM [Employees$] where emp_id=

JET

SELECT CSTR ([Subject ID]) as [Subject ID] FROM [Employees$] where emp_id=
Raj More
+3  A: 

Call the DataSet's GetXml() method.

MusiGenesis
A: 

Built a string as i read each row and was able to output it successfully

while (reader.Read())

            {
               string str="";

                str = str + reader[4].ToString().PadLeft(10) + '|' +

reader[5].ToString().PadLeft(1) + '|' + reader[6].ToString() + '\n';

            }