Click here to Skip to main content
12,629,804 members (28,046 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi All,
Iam creating datarows and show that values in gridview.Now i want to save that values in database.
This is my code:
I called this below function in button click.

Protected Sub BindGridviewData()
        Dim dt As New DataTable()
        dt.Columns.Add("UserId", GetType(Int32))
        dt.Columns.Add("FirstName", GetType(String))
        dt.Columns.Add("LastName", GetType(String))
        dt.Columns.Add("Education", GetType(String))
        dt.Columns.Add("Location", GetType(String))
        dt.Columns.Add("State", GetType(String))
        dt.Columns.Add("MobileNo", GetType(Long))
        Dim dtrow As DataRow = dt.NewRow()
        dtrow("UserId") = 1
        dtrow("FirstName") = "Hari"
        dtrow("LastName") = "I"
        dtrow("Education") = "MCA"
        dtrow("Location") = "Vizianagaram"
        dtrow("State") = "AndhraPradesh"
        dtrow("MobileNo") = 9966246869
        dt.Rows.Add(dtrow)
        dtrow = dt.NewRow()
        dtrow("UserId") = 2
        dtrow("FirstName") = "Vishak"
        dtrow("LastName") = "Nair"
        dtrow("Education") = "BCA"
        dtrow("Location") = "Kochi"
        dtrow("State") = "Kerala"
        dtrow("MobileNo") = 8886355702
        dt.Rows.Add(dtrow)
        dtrow = dt.NewRow()
        dtrow("UserId") = 3
        dtrow("FirstName") = "Samba"
        dtrow("LastName") = "Nallamala"
        dtrow("Education") = "B.Tech"
        dtrow("Location") = "Tirupathi"
        dtrow("State") = "AndhraPradesh"
        dtrow("MobileNo") = 9954786688
        dt.Rows.Add(dtrow)
        dtrow = dt.NewRow()
        dtrow("UserId") = 4
        dtrow("FirstName") = "Venkat"
        dtrow("LastName") = "K"
        dtrow("Education") = "B.Tech"
        dtrow("Location") = "Hyderabad"
        dtrow("State") = "AndhraPradesh"
        dtrow("MobileNo") = 7356588688
        dt.Rows.Add(dtrow)
        editgrid.DataSource = dt
        editgrid.DataBind()
    End Sub

Thanks&Regards
Hari
Posted 4-Jan-13 1:16am
Updated 4-Jan-13 1:25am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Refer Below Code..

foreach (GridViewRow GVRow in GridView1.Rows)
   {
       Name = GVRow.Cells[1].Text;
       CarType = GVRow.Cells[2].Text;
       TechnicalNo = GVRow.Cells[3].Text;
       ProductionDate = GVRow.Cells[4].Text;
       EngaineType = GVRow.Cells[5].Text;
       NoInStock = GVRow.Cells[6].Text;
       NoForCar = GVRow.Cells[7].Text;
       Price = GVRow.Cells[8].Text;
       Image = GVRow.Cells[9].Text;
       Desc = GVRow.Cells[10].Text;
       PartType = GVRow.Cells[11].Text;
       Level = GVRow.Cells[12].Text;
       Unit = GVRow.Cells[13].Text;
       Ratio = GVRow.Cells[14].Text;
       Dirham = GVRow.Cells[15].Text;
       ExtraMoney = GVRow.Cells[16].Text;
 

   SqlConnection scn = new SqlConnection(clspublic.GetConnectionString());
using(con)
{
   SqlCommand scm = new SqlCommand();
   scm.Connection = scn;
   scm.CommandText = @"INSERT INTO tblProduct
                     (fName, fxCarType, fProductionDate, fEngineType, fNoinStock, fNoforCar, fPrice,fRatio,fDirham,fExtraMoney, fImage, fDesc, fxPartType, fxLevel,fUnitType,fTechnicalNo)
          VALUES     (@fName,@fxCarType,@fProductionDate,@fEngineType,@fNoinStock,@fNoforCar,@fPrice,@fRatio,@fDirham,@fExtraMoney,@fImage,@fDesc,@fxPartType,@fxLevel,@fUnitType,@fTechnicalNo)";
 
   scm.Parameters.AddWithValue("@fName", Name.ToString());
   scm.Parameters.AddWithValue("@fxCarType", CarType.ToString());
   scm.Parameters.AddWithValue("@fTechnicalNo", TechnicalNo.ToString());
   scm.Parameters.AddWithValue("@fProductionDate", ProductionDate.ToString());
   scm.Parameters.AddWithValue("@fEngineType", EngaineType.ToString());
   scm.Parameters.AddWithValue("@fNoinStock", NoInStock.ToString());
   scm.Parameters.AddWithValue("@fNoforCar", NoForCar.ToString());
   scm.Parameters.AddWithValue("@fPrice", Price.ToString());
   scm.Parameters.AddWithValue("@fRatio", Ratio.ToString());
   scm.Parameters.AddWithValue("@fDirham", Dirham.ToString());
   scm.Parameters.AddWithValue("@fExtraMoney", ExtraMoney.ToString());
   scm.Parameters.AddWithValue("@fImage", Image.ToString());
   scm.Parameters.AddWithValue("@fDesc", Desc.ToString());
   scm.Parameters.AddWithValue("@fxPartType", PartType.ToString());
   scm.Parameters.AddWithValue("@fUnitType", Unit.ToString());
   scm.Parameters.AddWithValue("@fxLevel", Level.ToString());
 
  scm.ExecuteNonQuery();
}
  Permalink  
Comments
   
i can't understand .Please explain briefly
   
Thanq Prasanthi

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100