Click here to Skip to main content
15,896,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using two textboxs with sql as backend. if textbox1 values is selected then value will be updated on textbox2. Can Any tell how to insert values. Thanks in advance.
Posted
Updated 5-Jul-11 3:42am
v3
Comments
Simon_Whale 5-Jul-11 9:41am    
Email address removed in Question - Replies are always sent to your email account that you setup with

Code on .vb file

Imports System.Collections.Generic
Imports System.Web.Services
Imports ENTech.WebControls
Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Private Shared Function GetCityMenuItemsFromDataReader(ByVal reader As SqlDataReader, _
ByVal usePaging As Boolean, _
ByVal pageIndex As Integer, _
ByVal pageSize As Integer, _
ByVal appendCountryName As Boolean) As Generic.List(Of AutoSuggestMenuItem)
Dim menuItems As New Generic.List(Of AutoSuggestMenuItem)

Dim city As String
Dim cityCode As String
Dim state As String
Dim country As String

Dim label As String

Dim menuItem As AutoSuggestMenuItem
Dim rowIndex As Integer = 0

'Handle paging
Dim startRowIndex As Integer = 0
Dim endRowIndex As Integer = 0

If usePaging Then
startRowIndex = pageIndex * pageSize
endRowIndex = startRowIndex + pageSize
End If


While reader.Read()
If usePaging Then
'Get to the start of the page
If rowIndex < startRowIndex Then
rowIndex = rowIndex + 1
Continue While


'Break out of the loop if end of the page
If rowIndex >= endRowIndex Then
Exit While
End If
End If
End If


'Build label using City, Country & State
city = reader.GetString(0)
cityCode = reader.GetString(1)
country = reader.GetString(2)

If IsDBNull(reader.GetValue(3)) Then
state = ""
Else
state = reader.GetString(3)
End If

label = city

'Append either city or country
If (state <> "") Then
label &= " "
Else
If appendCountryName Then
label &= " "
End If
End If

menuItem = New AutoSuggestMenuItem()
menuItem.Label = label
menuItem.Value = cityCode

menuItems.Add(menuItem)

rowIndex = rowIndex + 1
End While


GetCityMenuItemsFromDataReader = menuItems
End Function

Private Shared Function GetConnectionString() As String

Dim connString As String = "connection string"

GetConnectionString = connString
End Function



<WebMethod()> _
Public Shared Function GetCitySuggestions(ByVal keyword As String, _
ByVal countryCode As String, _
ByVal usePaging As Boolean, _
ByVal pageIndex As Integer, _
ByVal pageSize As Integer) As String

Dim menuItems As Generic.List(Of AutoSuggestMenuItem)

Dim connString As String = GetConnectionString()
Dim cn As SqlConnection = New SqlConnection(connString)

'May need to repeat the following 3 times
'Instead just save it to variable
Dim sqlFromAndWhere As String = " FROM (product INNER JOIN brand ON product.productcate=brand.productcate) " & _
" LEFT OUTER JOIN vendor ON product.id=vendor.id " & _
" WHERE (product.productcate LIKE '" & keyword.Replace("'", "''") & "%')"

'Append country code to WHERE clause if specified
If Not String.IsNullOrEmpty(countryCode) Then
sqlFromAndWhere &= " brand.productcate='" & countryCode & "'"
End If


Dim sql As String

If usePaging Then
'Select only menu items up to specified page
'In Sql Server use ROW_NUMBER to only get the values for current page
Dim numItems As Integer = (pageIndex + 1) * pageSize
sql = "SELECT product.productcate as CityName, " & _
"product.productunit as CityCode, " & _
"brand.productcate as CountryName, " & _
"vendor.vendorname as StateName " & _
sqlFromAndWhere & _
" ORDER BY product.productcate"
Else

sql = "SELECT product.productcate as CityName, " & _
"product.productunit as CityCode, " & _
"brand.productcate as CountryName, " & _
"vendor.vendorname as StateName " & _
sqlFromAndWhere & _
" ORDER BY product.productcate"
End If


Dim cmd As SqlCommand = New SqlCommand(sql, cn)
cn.Open()

'I use datareader because it is usually much faster then dataSet
'But cached DataSet may also work
Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim appendCountryName As Boolean = String.IsNullOrEmpty(countryCode)
menuItems = GetCityMenuItemsFromDataReader(reader, usePaging, pageIndex, pageSize, appendCountryName)

reader.Close()

'When using paging need to get totalResults
Dim totalResults As Integer = -1

If usePaging And pageIndex = 0 Then
'Only do it when page index is 0
sql = "SELECT COUNT(*)" & sqlFromAndWhere

cmd = New SqlCommand(sql, cn)
totalResults = CType(cmd.ExecuteScalar(), Integer)
End If

cn.Close()

GetCitySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, totalResults)
End Function

<WebMethod()> _
Public Shared Function GetCountrySuggestions(ByVal keyword As String, ByVal usePaging As Boolean, ByVal pageIndex As Integer, ByVal pageSize As Integer) As String
Dim menuItems As New Generic.List(Of AutoSuggestMenuItem)

Dim connString As String = GetConnectionString()
Dim cn As SqlConnection = New SqlConnection(connString)

Dim sql As String = "SELECT productunit, productcate FROM product " & _
" WHERE productunit LIKE '" & keyword.Replace("'", "''") & "%'" & _
" ORDER BY productunit"


Dim cmd As New SqlCommand(sql, cn)
cn.Open()

Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim menuItem As AutoSuggestMenuItem

While reader.Read()
menuItem = New AutoSuggestMenuItem()

menuItem.Label = reader.GetString(0)
menuItem.Value = reader.GetString(1)

menuItems.Add(menuItem)
End While


reader.Close()

cn.Close()
GetCountrySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, -1) 'Total results (-1) is ignored if paging is not used }
End Function


<WebMethod()> _
Public Shared Function GetCountryInfo(ByVal cityCode As String) As String
Dim writer As New XJsonWriter()

Dim connString As String = GetConnectionString()
Dim cn As New SqlConnection(connString)

Dim sql As String = "SELECT product.productcate, product.productunit, brand.productcate as CityName " & _
" FROM product INNER JOIN brand ON brand.id=product.id " & _
" WHERE product.productunit='" & cityCode & "'"

Dim cmd As New SqlCommand(sql, cn)
cn.Open()

Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.Read() Then
writer.WriteNameValue("countryCode", reader.GetString(0))
writer.WriteNameValue("countryName", reader.GetString(1))
writer.WriteNameValue("cityName", reader.GetString(2))
Else
Throw New ArgumentException("City code '" & cityCode & "' is not valid", "cityCode")
End If

reader.Close()
cn.Close()

Return writer.ToString()
End Function
End Class
 
Share this answer
 
Code on .aspx

XML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register TagPrefix="Custom" Namespace="ENTech.WebControls" Assembly="AutoSuggestMenu" %>
<%@ Register namespace="ENTech.WebControls" tagprefix="WebControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .note { FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: Verdana, helvetica, 'sans serif' }
    </style>
    <script src="javascript/autocomplete.js" type="text/javascript"></script>
    <link href="Custom.css" rel="stylesheet" type="text/css" />
    <link href="asm_includes/AutoSuggestMenu.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style3
        {
            width: 40px;
        }
        .style4
        {
            width: 40px;
            font-weight: bold;
        }
        .style5
        {
            text-align: left;
        }
    p.MsoNormal
    {margin-top:0in;
    margin-right:0in;
    margin-bottom:10.0pt;
    margin-left:0in;
    line-height:115%;
    font-size:large;
    font-family:"Calibri","sans-serif";
            width: 99px;
            height: 20px;
        }
        .style6
        {
            width: 156px;
            text-align: left;
        }
        .style7
        {
            width: 155px;
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="t1" runat="server"></asp:TextBox>
                    <span style="font-size:11.0pt;line-height:115%;
font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
Calibri;mso-fareast-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:&quot;Times New Roman&quot;;mso-bidi-theme-font:minor-bidi;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA">
                    <custom:AutoSuggestMenu ID="asmCity" runat="server" KeyPressDelay="50"
                        MaxSuggestChars="6" OnClientTextBoxUpdate="onCityTextBoxUpdate"
                        OnGetSuggestions="onGetCitySuggestions" TargetControlID="t1"
                        UsePageMethods="false" />
                    </span>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="t2" runat="server"></asp:TextBox>
                    <span style="font-size:11.0pt;line-height:115%;
font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
Calibri;mso-fareast-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:&quot;Times New Roman&quot;;mso-bidi-theme-font:minor-bidi;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA">
                    <custom:AutoSuggestMenu ID="asmCountry" runat="server"
                        OnGetSuggestions="GetCountrySuggestions" TargetControlID="t2"
                        Visible="False" />
                    </span>
                    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="True">
                    </asp:ScriptManager>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 
Share this answer
 

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