473,410 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

One data source for multiple tables - dynamically changing

Hello,

On my page, I have only one SQL data source and one GridView. I would like to use My Data Source is for different tables I have in the database. SELECT statement works fine. However, when I try to update the data in any other table except the first/default table of datasource then my page for some reason shows the rows of first/default table.

In the 2nd scenario if I remove the "SELECTCOMMAND" and "UPDATECOMMAND" from SQLDataSource on the "aspx" page code view, then I click the Edit button for the first time it doesnt show the gridview. But when for the second time, I select another table from the drop down then it shows that table in the edit mode. So when I try to update the row, I get the following error.

“Updating is not supported by data source 'myDataSource' unless UpdateCommand is specified.”

If you see my code, I am updating the UpdateCommand dynamically.

I am just curious if one data source works for one table specifically. What’s the best approach for doing what I described above.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub ddlTable_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTable.SelectedIndexChanged
  2.         Dim strSelect, strColumns As String
  3.  
  4.         strColumns = ""
  5.  
  6.         Select Case ddlTable.SelectedValue
  7.             Case "businesslistings"
  8.                 strColumns = "*"
  9.                 myDataSource.UpdateCommand = "UPDATE businesslistings SET Title=@Title, Address=@Address,Phone=@Phone, Description=@Description WHERE ID=@ID"
  10.                 myDataSource.DeleteCommand = "Delete businesslistings WHERE ID=@ID"
  11.             Case "jobs"
  12.                 strColumns = "*"
  13.                 myDataSource.UpdateCommand = "UPDATE jobs SET Title=@Title, Location=@Location,Compensation=@Compensation, Description=@Description,Phone=@Phone WHERE ID=@ID"
  14.                 myDataSource.DeleteCommand = "Delete jobs WHERE ID=@ID"
  15.             Case "rentals"
  16.                 strColumns = "*"
  17.                 myDataSource.UpdateCommand = "UPDATE rentals SET Name=@Name, Address=@Address,Phone=@Phone, Email=@Email,Rent=@Rent, Description=@Description ID=@ID"
  18.                 myDataSource.DeleteCommand = "Delete rentals WHERE ID=@ID"
  19.             Case "sales"
  20.                 strColumns = "*"
  21.                 myDataSource.UpdateCommand = "UPDATE sales SET Name=@Name, Item=@Item,Phone=@Phone, Email=@Email,Price=@Price, Description=@Description WHERE ID=@ID"
  22.                 myDataSource.DeleteCommand = "Delete sales WHERE ID=@ID"
  23.         End Select
  24.  
  25.  
  26.         strSelect = "SELECT " & strColumns & " FROM " + ddlTable.SelectedValue
  27.         myDataSource.SelectCommand = strSelect
  28.  
  29.         lblMessage.Text = myDataSource.SelectCommand & "<br>" & myDataSource.UpdateCommand & "<br>" & myDataSource.DeleteCommand
  30.         'Response.Write(strSelect)
  31.     End Sub
  32.  
  33.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  34.         If Session("loginSuccessful") = False Then
  35.             Response.Redirect("login.aspx")
  36.         End If
  37.  
  38.         If Not IsPostBack Then
  39.             myDataSource.SelectCommand = "SELECT Title,Address,Phone,Description FROM businesslistings"
  40.             myDataSource.UpdateCommand = "UPDATE businesslistings SET Title=@Title, Address=@Address,Phone=@Phone, Description=@Description WHERE ID=@ID"
  41.             myDataSource.DeleteCommand = "Delete businesslistings WHERE ID=@ID"
  42.             lblMessage.Text = "Postback " & IsPostBack & "<br>" & myDataSource.SelectCommand & "<br>" & myDataSource.UpdateCommand & "<br>" & myDataSource.DeleteCommand
  43.         End If
  44.  
  45.     End Sub
  46.  
Mar 27 '13 #1
0 1245

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
6
by: ApexData | last post by:
I have 2 tables: Table1 and Table2. Neither one has a primary key because each table will only have 1-record. My form is a SingleForm unbound with tabs (my desire here). Using this form, in...
2
by: rlish | last post by:
I have a database with multiple tables that are all linked by a "site id" field. For any given record the site id field is identical within all the tables. Is there anyway to create a form with a...
2
by: chopin | last post by:
I am using Microsoft Access, and VBA. I was wondering if it was possible to select multiple tables using DAO. For example, here is the code I am thinking should work, but doesn't: sSQL =...
0
by: dilchahatahai | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports Microsoft Public Class Form1 'Dim rpt As New final() 'The report you created. Dim rpt As New CrystalReport1()...
5
by: Kimg0123 | last post by:
I would like to be able to append data from multiple tables into a single summary table using either an event procedure for the On Load or On Open properties on a form, but cannot seem to figure this...
1
by: 5095 | last post by:
Hi, i want to import data in multiple tables from a single text file (each field is seprated by pipe sign and each record is seprated by asterisk * ). Please guide me to the appropriate way. ...
1
by: varunkumarid | last post by:
hai to one and all... I am using The VB.Net 2005 and SQL server2000 .. I need to insert the Data into multiple tables at once... if you know about this Concept please Reply quick
8
by: obtrs | last post by:
show data of multiple tables? i have 3 tables i want to show the data from them to a page. table1 "trip" table2 "seat" table3 "user_information" show all the data of table one which is...
0
by: NareshN | last post by:
Hi All, I have this tables.I want to upload excel file data to multiple tables. Excel file consist of EmpName,1 week dates with data(TimeIn and TimeOut). I want to insert EmpName into...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
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
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 project—planning, coding, testing,...

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.