V
virlinz
Hello
I'm a newbie who has a problem with updating the dataset into the
database. Maybe I missed a few lines of codes. Please shed some light
for me. The following code is working but not like I wanted.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dsn As String = ConfigurationSettings.AppSettings("dsn")
oConn = New OleDbConnection(dsn)
Dim intKey As Integer = CInt(Request.QueryString("id"))
BindData(intKey)
End Sub
Private Sub BindData(ByVal int As Integer)
strQ = "SELECT Name, Company, WorkAddress, DirectLine, ID
FROM Ad WHERE ID = " & intKey
oDaAd = New OleDbDataAdapter(strQ, oConn)
oCbAd = New OleDbCommandBuilder(oDaAd)
oDaAd.InsertCommand = oCbAd.GetInsertCommand
oDaAd.UpdateCommand = oCbAd.GetUpdateCommand
Try
oConn.Open()
oDsAd.Clear()
oDaAd.MissingSchemaAction = MissingSchemaAction.AddWithKey
oDaAd.Fill(oDsAd, "Ad")
txtCoName.DataBind()
txtPIC.DataBind()
txtDirectLine.DataBind()
Catch ex As Exception
Label1.Text = ex.ToString()
Finally
If oConn.State = ConnectionState.Open Then
oConn.Close()
End If
End Try
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Dim dr As DataRow = oDsAd.Tables(0).Rows.Find(intKey)
dr("Name") = txtPIC.Text
dr("Company") = txtCoName.Text
dr("DirectLine") = txtDirectLine.Text
Try
oConn.Open()
oDaAd.Update(oDsAd, "Ad")
Catch ex As Exception
Label1.Text = ex.ToString()
Finally
oConn.Close()
End Try
With DataGrid1
.DataSource = oDsAd
.DataBind()
End With
In the aspx page
<asp:textbox id=txtPIC style="Z-INDEX: 126; LEFT: 200px; POSITION:
absolute; TOP: 88px" tabIndex=3 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].Name") %>'
Width="358px"></asp:textbox>
<asp:textbox id=txtCoName style="Z-INDEX: 124; LEFT: 200px; POSITION:
absolute; TOP: 56px" tabIndex=1 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].Company") %>'
Width="358px"></asp:textbox>
<asp:textbox id=txtDirectLine style="Z-INDEX: 133; LEFT: 200px;
POSITION: absolute; TOP: 424px" tabIndex=11 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].DirectLine") %>'
After executing the codes, the dataset doesnt have the changes that I
have made in the textboxes i.e they contains the original values. But
when I made the following changes, the columns of the table is updated
with the values they are assigned to.
dr("Name") = "me"
dr("Company") = "myCo"
dr("DirectLine") = 1234
Where did I done wrong? Is it because of the binding? I need to bind it
because I'm populationg the controls with the existing data from the
database.
I thank you in advance for your help.
I'm a newbie who has a problem with updating the dataset into the
database. Maybe I missed a few lines of codes. Please shed some light
for me. The following code is working but not like I wanted.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dsn As String = ConfigurationSettings.AppSettings("dsn")
oConn = New OleDbConnection(dsn)
Dim intKey As Integer = CInt(Request.QueryString("id"))
BindData(intKey)
End Sub
Private Sub BindData(ByVal int As Integer)
strQ = "SELECT Name, Company, WorkAddress, DirectLine, ID
FROM Ad WHERE ID = " & intKey
oDaAd = New OleDbDataAdapter(strQ, oConn)
oCbAd = New OleDbCommandBuilder(oDaAd)
oDaAd.InsertCommand = oCbAd.GetInsertCommand
oDaAd.UpdateCommand = oCbAd.GetUpdateCommand
Try
oConn.Open()
oDsAd.Clear()
oDaAd.MissingSchemaAction = MissingSchemaAction.AddWithKey
oDaAd.Fill(oDsAd, "Ad")
txtCoName.DataBind()
txtPIC.DataBind()
txtDirectLine.DataBind()
Catch ex As Exception
Label1.Text = ex.ToString()
Finally
If oConn.State = ConnectionState.Open Then
oConn.Close()
End If
End Try
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Dim dr As DataRow = oDsAd.Tables(0).Rows.Find(intKey)
dr("Name") = txtPIC.Text
dr("Company") = txtCoName.Text
dr("DirectLine") = txtDirectLine.Text
Try
oConn.Open()
oDaAd.Update(oDsAd, "Ad")
Catch ex As Exception
Label1.Text = ex.ToString()
Finally
oConn.Close()
End Try
With DataGrid1
.DataSource = oDsAd
.DataBind()
End With
In the aspx page
<asp:textbox id=txtPIC style="Z-INDEX: 126; LEFT: 200px; POSITION:
absolute; TOP: 88px" tabIndex=3 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].Name") %>'
Width="358px"></asp:textbox>
<asp:textbox id=txtCoName style="Z-INDEX: 124; LEFT: 200px; POSITION:
absolute; TOP: 56px" tabIndex=1 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].Company") %>'
Width="358px"></asp:textbox>
<asp:textbox id=txtDirectLine style="Z-INDEX: 133; LEFT: 200px;
POSITION: absolute; TOP: 424px" tabIndex=11 runat="server" Text='<%#
DataBinder.Eval(oDsAd, "Tables[Ad].DefaultView.[0].DirectLine") %>'
After executing the codes, the dataset doesnt have the changes that I
have made in the textboxes i.e they contains the original values. But
when I made the following changes, the columns of the table is updated
with the values they are assigned to.
dr("Name") = "me"
dr("Company") = "myCo"
dr("DirectLine") = 1234
Where did I done wrong? Is it because of the binding? I need to bind it
because I'm populationg the controls with the existing data from the
database.
I thank you in advance for your help.