In this article, we will show some tooltips on textboxes and the tooltip text will come through our SQL Database tables for each textbox. We will achieve this using the following agenda.

Thinking it to be very difficult, hold on...first read the article then you will come to know, how easy it is.

You can download the source code here.

Solution

Step 1:
Create database with some sample data. You can use your own data or use the following script.

  1. CREATETABLEdbo.TooltipEmployees(controlNameNVARCHAR(100) NULL,
  2. tooltipDataNVARCHAR(100) NULL);
  3. INSERTINTOdbo.TooltipEmployees(controlName,
  4. tooltipData)
  5. VALUES(N 'Name', N 'PleaseenterasinIdproof');
  6. INSERTINTOdbo.TooltipEmployees(controlName,
  7. tooltipData)
  8. VALUES(N 'Salary', N 'Enterasperpayslip');
  9. INSERTINTOdbo.TooltipEmployees(controlName,
  10. tooltipData)
  11. VALUES(N 'EmailId', N 'Enterofficialemail');
  12. INSERTINTOdbo.TooltipEmployees(controlName,
  13. tooltipData)
  14. VALUES(N 'Designation', N 'Enterasperpayslip');
  15. CREATEPROCspGetTooltipData@ controlNameNVARCHAR(100)
  16. AS
  17. BEGIN
  18. SELECTte.*
  19. FROMdbo.TooltipEmployeeste
  20. WHEREte.controlName = @controlName;
  21. END;
Our database table will look like the following:

result

Step 2:
Create an Empty ASP.NET Web application.

empty web application

empty templete

Step 3:
Add a class file.

add new item

Step 4: Replace with the following code.
  1. namespaceAsp.netTooltipUsingJqueryUI {
  2. publicclassTooltip {
  3. publicstringcontrolName {
  4. get;
  5. set;
  6. }
  7. publicstringtooltipData {
  8. get;
  9. set;
  10. }
  11. }
  12. }
Step 5: Add a new web service file.

add webservice

Step 6: Replace with the following code.
  1. usingSystem.Configuration;
  2. usingSystem.Data;
  3. usingSystem.Data.SqlClient;
  4. usingSystem.Web.Script.Serialization;
  5. usingSystem.Web.Services;
  6. namespaceAsp.netTooltipUsingJqueryUI {
  7. [WebService(Namespace = "http://tempuri.org/")]
  8. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  9. [System.ComponentModel.ToolboxItem(false)]
  10. [System.Web.Script.Services.ScriptService]
  11. publicclassTooltipDataService: System.Web.Services.WebService {
  12. [WebMethod]
  13. publicvoidGetTooltipData(stringcontrolName) {
  14. stringCS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  15. TooltiptoolTip = newTooltip();
  16. using(SqlConnectioncon = newSqlConnection(CS)) {
  17. con.Open();
  18. SqlCommandcmd = newSqlCommand("spGetTooltipData", con);
  19. cmd.CommandType = CommandType.StoredProcedure;
  20. cmd.Parameters.AddWithValue("@controlName", controlName);
  21. SqlDataReaderdr = cmd.ExecuteReader();
  22. while (dr.Read()) {
  23. toolTip.controlName = dr["controlName"].ToString();
  24. toolTip.tooltipData = dr["tooltipData"].ToString();
  25. }
  26. JavaScriptSerializerserializer = newJavaScriptSerializer();
  27. Context.Response.Write(serializer.Serialize(toolTip));
  28. }
  29. }
  30. }
  31. }
Note: Go to Web.config file and add the connection strings to your database.
  1. <connectionStrings>
  2. <addconnectionString="DataSource=.;InitialCatalog=DemoDB;IntegratedSecurity=True"name="DBCS"providerName="System.Data.SqlClient"/>
  3. </connectionStrings>
Explanation of Web Service code:

Step 7: Now let's check our webservice, if it is returning correct data from database or not. Press Ctrl + F5.

returning correct data from database

get tooltip

run

Now you can see that our web service is returning correct data in JSON format.

Step 8: Add a new Web form to implement our front end code.

add web form

Step 9: Go to source mode of Web form and add the following code in the body tag.

  1. <tableborder="1">
  2. <tr>
  3. <td><b>Name:</b></td>
  4. <td>
  5. <inputtype="text" title="" id="Name" class="tooltips" />
  6. </td>
  7. </tr>
  8. <tr>
  9. <td><b>Salary:</b></td>
  10. <td>
  11. <inputtype="text" title="" id="Salary" class="tooltips" />
  12. </td>
  13. </tr>
  14. <tr>
  15. <td><b>EmailId:</b></td>
  16. <td>
  17. <inputtype="text" title="" id="EmailId" class="tooltips" />
  18. </td>
  19. </tr>
  20. <tr>
  21. <td><b>Designation:</b></td>
  22. <td>
  23. <inputtype="text" title="" id="Designation" class="tooltips" />
  24. </td>
  25. </tr>
  26. </table>
Note: The id of each of the HTML element should be same that you have stored in your database.

Step 10: To add the jQuery references, visit my article here and follow step 5 and step 9 to download jQueryUI files and add their references or you can just download the code file attached with this article.

Step 11: Add the following jQuery code to the head section.
  1. < scripttype = "text/javascript" > $(document).ready(function() {
  2. $('.tooltips').tooltip({
  3. content: getTooltipText
  4. });
  5. functiongetTooltipText() {
  6. varrtnValue = '';
  7. $.ajax({
  8. method: "POST",
  9. dataType: "json",
  10. url: "TooltipDataService.asmx/GetTooltipData",
  11. data: {
  12. controlName: $(this).attr('id')
  13. },
  14. async: false,
  15. success: function(data) {
  16. rtnValue = data.tooltipData;
  17. }
  18. });
  19. returnrtnValue;
  20. }
  21. }); < /script>
Explanation of jQuery code:

Step 12: Now save all the changes, press Ctrl + F5 and you will see the following:

grid view

enter details

grid

enter official email

enter as per payslip

Have you ever wondered how easy to do it using jQuery.

Hope you enjoyed this!