473,509 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
0 2171

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2191
by: Christopher | last post by:
I AM GETTING A SYSTEM.DATA.SQLCLIENT.SQLEXCEPTION ERROR WHEN ATTEMPTING TO INSERT DATA INTO A SINGLE TABLE THROUGH A GRID //If this is due to a spelling error, i will //inflict a ritual...
5
7014
by: SSP | last post by:
Dear ASP.NETers, How would I insert multiple rows of data from a web form? Are there any tute's and stuff around. Couldn't find any myself. Thanks in advance. SSP
1
1664
by: tshad | last post by:
I can't seem to get insert into a DataGrid to work. I am using an example from the net and made changes to work on my computer. The grid comes up fine. The edit boxes in the footer are there. ...
2
1691
by: Stuart | last post by:
Hi there I am using the following to take an account number from a datagrid in to another page and use it to load specific details according to the account number: Public Sub...
0
1517
by: Eustice Scrubb | last post by:
In line coding problem. Here's my code: <script language="VB" runat="server"> Dim myConnection As SqlConnection Sub Page_Load(Src As Object, e As EventArgs) ' Create a connection to the SQL...
1
1324
by: schapopa | last post by:
Hi Everybody, I have a datagrid and I am using sqlcommandbuilder for update and insert of the data. I have two questions related to insert new row in datagrid. When I get to the new row how...
3
2005
by: pjcraig | last post by:
This is driving me crazy! I have a form that a user will access from another form by selecting the item that they wish to view. When they open the new form, I pass through the id of the item they...
1
3070
by: glenn | last post by:
Hi folks, I am using an Access database, VB.NET and ADO.NET working with a DataGrid control. MY datagrid table has both a date_sent and a date_ans field. When I Insert a record in my SQL...
0
2179
by: danishce | last post by:
Hello, I want to insert data directly into my windows form data grid and load a combobox(userid) in the 1st column of data grid,a textbox(password) in 2nd column of datagrid. The code for insert...
5
5469
by: Me LK | last post by:
I need to add a line to choose a size into a dropdown . Since I have the dropdown nested inside a datagrid this is not working.When a page displays there are several rows of dropdowns but only the...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.