Click here to Skip to main content
14,930,311 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been working on a bookstore management project. I have 5 different categories of book and 5 different tables for them.. So i have this data entry page created with textboxes for data and dropdown list-box for selecting the category/table name:
<tr><td style="text-align: left;color:blue; font-size:30px; font-family:Maiandra GD ">CATEGORY :</td><td> <asp:DropDownList ID="DropDownList1" CssClass="txt2" runat="server">
    <asp:ListItem>Academic & Professional</asp:ListItem>
    <asp:ListItem>Children Books</asp:ListItem>
    <asp:ListItem>College Books</asp:ListItem>
    <asp:ListItem>Entrance Books</asp:ListItem>
    <asp:ListItem>Literature & Fiction</asp:ListItem>

So i need help as to how using a single button i can insert into multiple tables?? The table should be chosen as per the selection in the dropdownlistbox..
The table names are same as the listitem

Suppose i select Entrance books then the data must be inserted into the Entrance books table.

Here's the common table structure for all the tables--
name varchar(20) not null primary key,
aurthor varchar(20),
price int,
details varchar(1000)

Arun Vasu 27-May-13 4:31am
use stored procedure to achieve this.
Jignesh Khant 27-May-13 4:32am
Yes you can use if else statement like this:
if(DropDownList1.SelectedValue="Academic & Professional")
//insert code
// insert code
Swayam231 27-May-13 4:36am
i have thought about that but it will require separate insert statements for each selection!!

Pass table name as parameter and use your stored procedure like this:
Create Procedure InsertData
@tablename varchar(50)
--Check Conditions

if @tablename = 'Academic & Professional'
--fire insert query for table 'Academic & Professional'

if @tablename = 'Children Books'
--fire insert query for table 'Children Books'

--check for all the tables
Swayam231 27-May-13 4:47am
thanks buddy!!!
You can execute a stored procedure or multiple sets of insert queries on button click.
You can put them in a transaction to rollback any dependent inserts.
Swayam231 27-May-13 4:32am
Can you be little more detailed??
Swayam231 27-May-13 4:40am
Here's my sample insert statement for inserting into single table:

SqlConnection cn = new SqlConnection();
cn.ConnectionString = //connection string
string st="Insert into entrance books values("+textbox1.text+"","+textbox2.text+","+textbox3.text+","+textbox4.text+")";
sqlcommand cmd= new sqlcommand(st,cn);
Abhinav S 27-May-13 4:48am
Something like
string st=Query1)"; sqlcommand cmd= new sqlcommand(st,cn); cmd.executenonquey;

string st=Query2; sqlcommand cmd= new sqlcommand(st,cn); cmd.executenonquey;

And so on.

Or you could use a stored procedure.
By the way, you should not directly pass in parameters in query.
Swayam231 27-May-13 4:50am
okk thanks for helping!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900