Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Can anyone please tell me how to import gridview data into database table in VisualStudio2008 using c#.
I tried,
C#
string a = GridView1.Rows[0][0].Tostring();

but got an error:
Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Web.UI.WebControls.GridViewRow'
Waiting for your response. Please help!

Here is my code which import excel data to gridview:
C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Office.Interop.Excel;
using System.Text;
using System.Reflection;
public partial class ImportDD : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = GetExcel("c:\\Details.xls");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    public DataSet GetExcel(string fileName)
    {
        Application oXL;
        Workbook oWB;
        Worksheet oSheet;
        Range oRng;
        try
        {
            //  creat a Application object
            oXL = new ApplicationClass();
            //   get   WorkBook  object
            oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);
            //   get   WorkSheet object
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
            System.Data.DataTable dt = new System.Data.DataTable("Details");
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            DataRow dr;
            StringBuilder sb = new StringBuilder();
            int jValue = oSheet.UsedRange.Cells.Columns.Count;
            int iValue = oSheet.UsedRange.Cells.Rows.Count;
            //  get data columns
            for (int j = 1; j <= jValue; j++)
            {
                dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
            }
            //  get data in cell
            for (int i = 1; i <= iValue; i++)
            {
                dr = ds.Tables["Details"].NewRow();
                for (int j = 1; j <= jValue; j++)
                {
                    oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
                    string strValue = oRng.Text.ToString();
                    dr["column" + j] = strValue;
                }
                ds.Tables["Details"].Rows.Add(dr);
            }
            return ds;
        }
        catch (Exception ex)
        {
            return null;
        }
        finally
        {
            Dispose();
        }
    }
}
Posted
Updated 14-Jun-10 18:14pm
v2

Hey ,
you can get the values through many ways depending on your cell type
TextBox or Label as Template Field or A Bounded Field ,

<br />
<br />
string a =GridView1.Rows[0].Cells[0].Text ;//if it bounded Field<br />
//--------------------------------------------------------------<br />
string b =(GridView1.Rows[0].Cells[0].Controls[1] as Label).Text ; //if it template field with only one label(only one control)<br />
//--------------------------------------------------------------<br />
<br />
string c=(GridView1.Rows[0].Cells[0].Controls[1] as TextBox).Text ;//if its template field with only one TextBox(only one control)<br />
<br />



regards

abraheem abulubbad
 
Share this answer
 
Comments
zarmeen 15-Jun-10 23:52pm    
Thnxs!
Its running fine.
string a =GridView1.Rows[0].Cells[0].Text ;
But can u plzz tell me how i insert or import values into my db table from gridview..bcoz my data is in excel and i want to import into db table.
Plzz help..
<br />
protected void lnkSave_Clock(object sender,EventArgs e)<br />
{<br />
//loop for each gv row ...<br />
 DataSet ds = GetExcel("c:\\Details.xls");<br />
foeach(DataRow row in ds.Tables[0])<br />
{<br />
insertRowToDB(row["coulmnName1"],row["coulmnName1"],row["coulmnName1"],row["coulmnName1"]);//you put how many row u have<br />
}<br />
}<br />
<br />
Private Void InsertRowToDb(object co1,object co2,object co3 ,object co4)//you put the same rows number (col5,col6...etc) that sent to the function<br />
{<br />
  SqlParameterCollection myPar = new SqlCommand().Parameters;<br />
        myPar.Add("@id", id);<br />
        myPar.Add("@type", type);<br />
        myPar.Add("@participantType", participantType);<br />
        myPar.Add("@participantID", participantID);<br />
        myPar.Add("@permissionDate", permissionDate);<br />
        myPar.Add("@startDate", startDate);<br />
        myPar.Add("@endDate", endDate);<br />
        myPar.Add("@cardType", cardType);<br />
        myPar.Add("@fn", fn);<br />
        return Abstract.executeNonQuery("p_InsertNewInterancePermission", myPar);<br />
}<br />
<br />
<br />
   public bool executeNonQuery(string command, SqlParameterCollection parameters)<br />
    {<br />
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ar-sa");<br />
        SqlConnection con = new SqlConnection("your connection string ");<br />
        SqlCommand com = new SqlCommand(command, con);<br />
        com.CommandType = CommandType.StoredProcedure;<br />
<br />
        for (int t = 0; t < parameters.Count; t++)<br />
        {<br />
            SqlParameter myParam = parameters[t];<br />
            parameters[t] = new SqlParameter();<br />
            com.Parameters.Add(myParam);<br />
        }<br />
<br />
        try<br />
        {<br />
            con.Open();<br />
            int s = com.ExecuteNonQuery();<br />
            con.Close();<br />
            if (s > 0)<br />
            {<br />
                return true;<br />
            }<br />
            else<br />
            {<br />
                return false;<br />
            }<br />
        }<br />
        catch (Exception)<br />
        {<br />
            con.Close();<br />
            return false;<br />
        }<br />
    }<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900