467,925 Members | 1,811 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

Insert a new row using asp.net datagrid

I am having problems with the following code. I am trying to update my db table Categories in Northwind. I have borrowed this code from www.dotnetjohn.com/articles/articleid27.aspx Although it works on dotnetjohn website, I cannot replicate the code successfully. I have a datagrid filled with data and two textboxes with an “Insert” button. When the button is pressed, the text entered into the two textboxes is to be added to the table Categories in Northwind db. I cannot get the click event to update the database and add a new record entry. The page_load fills the datagrid with data from a SELECT statement. The Insert_Click should take the entries from two textboxes and insert them into the Northwinds database Categories table. I have enclosed the basic components of my .aspx.vb page for the Page_Load, Insert_Click, and FilltheGrid() method. I am confused as to my problem. Please help. Thank you. If you need more data just ask me to provide it

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa

If Not Page.IsPostBack The
Dim dsCategories As DataSe
Dim temp As Object = Session("Categories"
If temp Is Nothing The
dsCategories = New DataSe
Dim sqlConn As String = "server=BP-ADOVE;database=northwind;uid=sa;pwd=
Dim selectText As Strin
selectText = "SELECT categoryName, Description FROM Categories WHERE (CategoryID = 1 or CategoryID=2 or CategoryID=3 or CategoryID=9)
Dim cmdCategories As SqlDataAdapter = New SqlDataAdapter(selectText, sqlConn
cmdCategories.Fill(dsCategories, "Categories"
Session("Categories") = dsCategorie

Els
dsCategories = CType(temp, DataSet
End I
dgAction.DataSource = dsCategories.Tables("Categories"
dgAction.DataBind(
End I
End Su

Sub btnInsert_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles btnInsertAction.Clic
If Page.IsValid The
Dim strDate As Strin
Dim strDesc As Strin

objConn = New SqlConnection(ConfigurationSettings.AppSettings("c onnectionString")
objConn.Open(
strDate = txtActionDate.Text.Trim(
strDesc = txtDescription.Text.Trim(
strSql = "INSERT INTO Employees (LastName, FirstName) "
& "VALUES ('" & strDate & "', '" & strDesc & "')
objCmd = New SqlCommand(strSql, objConn
objCmd.ExecuteNonQuery(

'Dim cmdCategories As SqlDataAdapter = New SqlDataAdapter(strSql, objConn
'cmdCategories.Fill(dsCategories, "Categories"
'dgAction.DataSource = dsCategories.Tables(0).DefaultVie
'dgAction.DataBind(

objCmd.ExecuteNonQuery(
objConn.Close(
FillTheGrid(

End I
End Su

Sub FillTheGrid(
'Dim adoComponent As ADOCOM.DataClass = New ADOCOM.DataClas
'Dim adoRS As ADODB._Recordset = adoComponent.GetData(

Dim myDA As SqlDataAdapter = New SqlDataAdapte
Dim ds As DataSet = New DataSe
myDA.InsertCommand = objCm

Dim dt As DataTable = ds.Tables("Employees"
myDA.TableMappings.Add("Employees", "Employees"

myDA.Update(ds, "Employees"

'dataAdapter.Fill(ds
dgAction.DataSource = ds.Tables("Employees") '.DefaultVie
dgAction.DataBind(

End Sub
Nov 22 '05 #1
  • viewed: 2013
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher | last post: by
1 post views Thread by tshad | last post: by
2 posts views Thread by Stuart | last post: by
reply views Thread by Eustice Scrubb | last post: by
1 post views Thread by schapopa | last post: by
3 posts views Thread by pjcraig | last post: by
1 post views Thread by glenn | last post: by
5 posts views Thread by Me LK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.