HOw to Save grid data to sql server?


#1

Hi

1-Actually my problem is that i want to save the data in grid to another database like sql server or in dataset so pls let me know how can i do?

2-secondlly how can i count the number of rows in aspose.grid?

like if user entered only 10 rows

Regards

Geeta


#2

Hi,

1.You may use WebCells.Export method to export sheet data to a DataTable object. After that you may build a SqlDataAdapter object to save the DataTable to SqlServer.

2.The WebCells.MaxRow property indicates the max row number of the cells.


#3

In the database, create table with an ID (ID int)field and an Image field (FStream Image). Create a stored proc to save the file as an image ("sp_AddReport"). Make it fancy by coming up with some scheme to get a Report ID...

In aspx.cs....

using Microsoft.ApplicationBlocks.Data;

...

//TO SAVE THE FILE:

int ReportID = 1;
try
{
MemoryStream mstream = new MemoryStream();
GridWeb1.WebWorksheets.SaveToExcelFile(mstream);

//buffer to hold Document Contents
byte[] Docbuffer = mstream.GetBuffer();

//sql string- create a stored proc to handle the two parameters
string sql = "sp_AddReport";

//parameters
SqlParameter[] myParams = new SqlParameter[2];
myParams[0] = new SqlParameter("@Report", Docbuffer);
myParams[1] = new SqlParameter("@ReportID", ReportID);

SqlHelper.ExecuteNonQuery(new SqlConnection(dbConnString), CommandType.StoredProcedure, sql, myParams);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
} finally {
if(myStream != null)myStream.Close();
}

//TO RETRIEVE THE FILE:

MemoryStream myStream = null;
try
{
byte[] myBites = (byte[])SqlHelper.ExecuteScalar(new SqlConnection(dbConnString), CommandType.Text, "Select FStream from Reports where ReportID = 1");
myStream = new MemoryStream(myBites,0,myBites.Length, true, true);

GridWeb1.WebWorksheets.Clear();
GridWeb1.WebWorksheets.ImportExcelFile(myStream);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally {
if(myStream != null)myStream.Close();
}


#4

That’s a nice idea. Thank you very much for sharing your code.