?
Solved

How can I edit/update data in any table in a given sql database?

Posted on 2012-03-10
6
Medium Priority
?
271 Views
Last Modified: 2012-04-07
I have a dropdown that selects a sql database.
a second dropdown that selects a table in a given database.
The code I have so far only works if I select table 'tblItems' in database 'Cams2'
If I select a different table it gives an error.
How can I edit/update data in any table in a given sql database?
The error is caused by the following code:
line 555       Me.DGrid.DataKeyField = "ItemID"
line 571       m_Key = DGrid.DataKeys(e.Item.ItemIndex)
line 575        Dim SQL As String = "Update " & strTable & " Set Item='" & tbox.Text & "' Where ItemID=" & m_Key
How can I re-write the code to accept DataKeyField  from different tables etc?
Thanks in advance for any help given, Ian.
for clarity below is the code so far:
Private Sub LoadData()
        Try
            Dim m_DBCon As New OleDbConnection()
            Dim Cstring As String = HttpContext.Current.Session("strConnection")
            If m_DBCon.State <> ConnectionState.Open Then
                m_DBCon.ConnectionString = Cstring
                m_DBCon.Open()
            End If
            Dim SQL As String
            Dim strTable As String = HttpContext.Current.Session("strTable")
            SQL = "Select * From " & strTable
            Dim DA As OleDb.OleDbDataAdapter
            Dim DS As DataSet
            DA = New OleDb.OleDbDataAdapter(SQL, m_DBCon)
            DS = New DataSet()
            DA.Fill(DS)
            Me.DGrid.DataSource = DS
            Me.DGrid.DataKeyField = "ItemID"
            Me.DGrid.DataBind()
        Catch ex As Exception
            ErrorLabel.Text = "=1:Error:558:LoadData:" & "Session=" & HttpContext.Current.Session("sqlCamsAdmin.AdminUser") & "#&nbsp;&nbsp;&nbsp;" & "strConnection=" & HttpContext.Current.Session("strConnection") & "#<br/>" & ex.ToString()
        End Try
    End Sub
    Private Sub DGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DGrid.UpdateCommand
        Response.Write("<div style='position:absolute;width:530px;height:166px;left:9px;top:87px;font-size:xx-small;overflow:auto;border:1px solid #722F90;' id='DIV17'>")
        Try
            Dim m_DBCon As New OleDbConnection()
            Dim Cstring As String = HttpContext.Current.Session("strConnection")
            If m_DBCon.State <> ConnectionState.Open Then
                m_DBCon.ConnectionString = Cstring
                m_DBCon.Open()
            End If
            Dim m_Key As Long
            m_Key = DGrid.DataKeys(e.Item.ItemIndex)
            Dim tbox As TextBox
            tbox = e.Item.FindControl("txtItem")
            Dim strTable As String = HttpContext.Current.Session("strTable")
            Dim SQL As String = "Update " & strTable & " Set Item='" & tbox.Text & "' Where ItemID=" & m_Key
            Response.Write("state:576:Update:" & m_DBCon.State() & "#<br/>")
            Response.Write("str:577:" & Cstring & "  " & strTable & "#<br/>")
            Response.Write("SQL:578:" & SQL & "#<br/>")
            Dim CMD As New OleDb.OleDbCommand(SQL, m_DBCon)
            CMD.ExecuteNonQuery()
            Me.DGrid.EditItemIndex = -1
            LoadData()
        Catch ex As Exception
            ErrorLabel.Text = "=1:Error:584:Update:" & "Session=" & HttpContext.Current.Session("sqlCamsAdmin.AdminUser") & "#&nbsp;&nbsp;&nbsp;" & "strConnection=" & HttpContext.Current.Session("strConnection") & "#<br/>" & ex.ToString()
        End Try
        Response.Write("</div>")
    End Sub
0
Comment
Question by:homeshopper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37705214
And what's the error? I think the issue might be that you have to specify the database in your connection string (or use "User dbname") so you are unable to update other databases.
0
 

Author Comment

by:homeshopper
ID: 37705228
Hi CodeCruiser,
If I selected say database 'Cams2' and table 'tblItems' no error.
if I selected a different table say 'ProductCategories'  error as follows:
label:=1:Error:558:LoadData:Session=#   strConnection=Provider=SQLOLEDB;Data Source=fuertemon4;User ID=sa;Password=sasa; Initial Catalog=Cams2;#
System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ItemID'. at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.BaseDataList.DataBind() at scriptFrame_tempScript2.LoadData() in C:\Projects\cams00CsVbDev\sqlScriptVbData\scriptFrame\tempScript2.aspx.vb:line 556
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37705944
You have to change the datakeyname to the one which exists in the table.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:homeshopper
ID: 37706448
Thankyou for your comments.
Is it possible to edit any selected table from a given database?
(either) without using datakeynames.
(or) programmically change the datakeyname to a field name that existing in the given table.
Hence, original question: How can I edit/update data in any table in a given sql database?
Thanks in advance for any help given.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1500 total points
ID: 37706956
You have

            DA.Fill(DS)
            Me.DGrid.DataSource = DS
            Me.DGrid.DataKeyField = "ItemID"
            Me.DGrid.DataBind()

So you can change the datakeyfield in code. You can either ask the user to enter the datakeyfield name in a textbox or add another dropdown which lists the column names form selected table and ask the user to choose one of the columns.
0
 

Author Comment

by:homeshopper
ID: 37763163
sorry, got waylaid on another project.
getting back to the matter in hand now.
I am going to use another dropdown for Columns as you suggested,
this will be poulated from the dropdown of the list of tables.
Thankyou for the help, I'll let you know who I get on later.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question