Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to get an idea for my requirement:
I have a asp.net web page to enter book details where user select book author from dropdownlist. There is a author master page from where we enter all the author names.But suppose there is a new author that is not in the dropdownlist then i want to give the user option to enter the new author name from the book entry page and that author gets inserted in database and added to dropdown automatically so that user can select that author. Please suggest me how to get this done.

What I have tried:

I think we can use web API for this..But no idea how to do this.
Posted
Updated 19-Nov-18 0:38am
Comments
Shashank Laxman 17-Nov-18 6:48am    
But user may not enter the entire name of book author,then whats the use of storing in database.

For eg : Amish Tripathi user may enter as Amish(for Amish Tripathi).

I suggest you to insert new author name in database and then fill all book details belonging to that author. This will place less burden on user.
F-ES Sitecore 17-Nov-18 7:32am    
We can't answer your question specifically because we don't know what language you're using, what presentation technology you're using, what database access technology you're using, what your database schema looks like or anything else really.

What you need to do is had a hidden text box for the "Other" on your form and give it a style of "display:none" so it isn't shown. Add an "Other" option to the dropdown of authors, you can give it a special value like "0" or "-1". Use javascript to listen to the onchange event of the dropdown and if it's "Other" (based on the value, not the text) make your hidden box appear by changing the style to "display:block", if it's not "Other" set the style to "display:hidden". Now when your form is selected, if the dropdown option is "Other" then read the text from the textbox and add it to your database, get the ID of the just-added entry and use that.

As you can see, if you break the problem down into steps there is a lot of work here. Take each step in turn and try and implement it, then if you have specific questions about specific problems feel free to ask.
xpertzgurtej 19-Nov-18 6:40am    
i am using asp.net and sql server. Thanks for giving the idea to solve my issue..many many thanks to you..
Laxmidhar tatwa technologies 19-Nov-18 0:38am    
you should display the code .basically. if you want to add from a textbox with button click then the code is DropDownList1.Items.Add(TextBox1.Text);

I agree with what F-ES Sitecore. What you can do is to add an optione "Others" in your DropDownList and once that option is selected, then you can place a TextBox and a Button in a Panel/Div to group them and then trigger that Div to be shown in the page. You have two options to do that if you are working on ASP.NET WebForms.

First option is using Server-side implementation, and that is to handle the DropDownList_SelectedIndexChanged event and on that event, you can check for the "Others" value and show your TextBox to the page where users can input data and then at Button_Click event, you can write a code to insert that data into your database. You don't need a Web API to implement this not unless if your data is coming from different server.

The second option is using Client-side implementation, could be plain JavaScript or jQuery. And you need to create a Web API or Page Method or Web Service to access your data via AJAX call. Here's a quick example using plain JavaScript:

HTML:
HTML
<select name="color" onchange='checkvalue(this.value)'> 
    <option>Select Authors</option>  
    <option value="1">You</option>
    <option value="2">Me</option>
    <option value="others">Others</option>
</select>
<div id="divNewEntry" style='display:none'> 
        <input type="text" id="txtNewAuthor" />
        <input type="button" id="btnSave" value="Save" /> 
</div>


JavaScript:
JavaScript
function checkvalue(val)
{
    if(val==="others")
       document.getElementById('divNewEntry').style.display='block';
    else
       document.getElementById('divNewEntry').style.display='none'; 
}


Once you've done that, you can then use jQuery AJAX to communicate with your server (API) to insert the database at Button's Click event something like this:

JavaScript
 $(document).ready(function () {  
            $('#btnSave').click(function () {  
                $.ajax({  
                    type: 'GET',  
                    url: 'api/Author',  
                    dataType: 'json',  
                    success: function (data) {  
  			// You can repopulate your dropdownlist here to reflect the newly added entry
			// then hide the Div here containing the TextBox and Button
                    }  
  
                });  
            });  
 
});  


Here's a great article that talks about how to access your data using jQuery AJAX: Many ways to communicate with your database using jQuery AJAX and ASP.NET[^]
 
Share this answer
 
Thanks for providing me the solution with 2 examples.. i am using asp.net and sql server and can go with any of these two solutions suggested by you.But i will prefer client side..thanks..
 
Share this answer
 
Comments
Vincent Maverick Durano 19-Nov-18 10:43am    
Cool. Just please don't use the Solution area when replying. You can use the "Have a Question or Comment" button instead.
xpertzgurtej 20-Nov-18 3:54am    
ok..thanks

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