searh ??? textbox1 and gridview :)

Last post 05-15-2008 8:43 AM by oguzkaygun. 15 replies.

Sort Posts:

  • searh ??? textbox1 and gridview :)

    05-14-2008, 2:02 PM

    hello friends

    i have button1, textbox1 and gridview1 and table1 .. i want gridview1 to display items that i search from textbox1.text with button1

    i want to select skills like insert skills below:) could you write to me as visual basic code ?

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim SglDataSource2, yeni As New SqlDataSource()

    SglDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString

    SglDataSource2.InsertCommandType = SqlDataSourceCommandType.Text

    SglDataSource2.InsertCommand = "INSERT INTO table1 (username, productname) VALUES (@username, @productname)"

    SglDataSource2.InsertParameters.Add("username", textbox1.Text)

    SglDataSource2.InsertParameters.Add("productname", textboxl2.Text)

    Dim rowsaffected As Integer = 0

    Try

    rowsaffected = SglDataSource2.Insert()

    Catch ex As Exception

    Server.Transfer("help.aspx")

    Finally

    SglDataSource2 = Nothing

    End Try

    If rowsaffected <> 1 Then

    Server.Transfer("help.aspx")

    Else

    Server.Transfer("continue.aspx")

    End If

    End Sub

    cheers

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-14-2008, 2:41 PM
    • Loading...
    • booler
    • Joined on 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 959

     You will need to execute some SQL like:

     

    SELECT  username, productname FROM table1 WHERE productname LIKE @productname + '%'
     

    Then use this as the SelectCommand on your datasource, and bind the results to your grid. Have a go, and post again if you run into any problems.

  • Re: searh ??? textbox1 and gridview :)

    05-14-2008, 4:22 PM

    hi

    i dont know how i use selectcommand and bind the results to grid..

    could you write to me as visualbacis code ?

    cheers

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-14-2008, 4:27 PM
    • Loading...
    • booler
    • Joined on 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 959

    Have a look throiugh this article- it contains all the information you need to retrieve and bind the results. 

  • Re: searh ??? textbox1 and gridview :)

    05-14-2008, 4:47 PM

    hi

    i have read this article before :)

    and i write visual basic code  for select skills.. but it doesnt work..

    cheers

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-14-2008, 5:39 PM
    • Loading...
    • booler
    • Joined on 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 959

    Can you post the code you have so far, and also describe in what way it does not work for you. Is there an error? If so, could you post the error details? 

  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 6:30 AM

    Hello my code is below but it doesnt work :)

    cheers 

    Protected
    Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim SglDataSource2 As New SqlDataSource()

    SglDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString

    SglDataSource2.SelectCommandType = SqlDataSourceCommandType.Text

    SglDataSource2.SelectCommand = "SELECT * FROM table1 WHERE username=textbox1.text"

    SglDataSource2.SelectParameters.Add("username", textbox1.Text)

    End Sub

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 6:42 AM
    • Loading...
    • booler
    • Joined on 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 959

     Your problem is the statement:

    "SELECT * FROM table1 WHERE username=textbox1.text"

    In this case, textbox1.text is a control property so it should not be included within the SQL. This should be replaced with a parameter (i.e.' @userName'), and you can then add the parameter value to the datasource command, i.e.

    SglDataSource2.SelectParameters.Add("@userName", textbox1.Text)

    So , in other words, 'username' is the field name in the database- this is a constant, '@userName' is a parameter which acts as a placeholder for the parameter value which you pass to the command, which in your case is the Text property of textbox1.

  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 6:48 AM
    • Loading...
    • vinkesh
    • Joined on 05-15-2008, 6:34 AM
    • Posts 38

    Hi,

            Please try below modified code. 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim SglDataSource2 As New SqlDataSource()

    SglDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString

    SglDataSource2.SelectCommandType = SqlDataSourceCommandType.Text

    SglDataSource2.SelectCommand = "SELECT * FROM table1 WHERE username=@user"

    SglDataSource2.SelectParameters.Add("@user", textbox1.Text)

    End Sub

     

    Please mark this post as "Answered" if it helps you

    Mark this post as "Answered" if it helps

    Thank You.
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 7:18 AM

    hello

    i have a error :) ProductID is int maybe it is error :) and my code is below

    i wait for your help :)

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim SglDataSource2 As New SqlDataSource()

    SglDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings("NORTHWNDConnectionString").ToString

    SglDataSource2.SelectCommandType = SqlDataSourceCommandType.Text

    SglDataSource2.SelectCommand = "SELECT * FROM products WHERE productID=@productID"

    SglDataSource2.SelectParameters.Add("@productID", TextBox1.Text)

    GridView1.DataSource = SqlDataSource2.SelectCommand

    GridView1.DataBind()

     

    End Sub

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 7:33 AM
    • Loading...
    • SolaimalaiVT
    • Joined on 06-12-2007, 7:00 AM
    • India
    • Posts 73

    HI,

        Check this Link
       

       This may be useful for you.

       Mark it as a ANSWER if it is useful for you
     

    With Regards,
    Solaimalai V.T
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 7:37 AM
    • Loading...
    • vinkesh
    • Joined on 05-15-2008, 6:34 AM
    • Posts 38

    Hi, 

        Set GridView1.DataSource = SqlDataSource2.ID

        GridView1.DataBind()

    Mark this post as "Answered" if it helps

    Thank You.
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 8:14 AM

    hi

    GridView1.DataSource = SqlDataSource2.ID    this code line have a mistake.. SqlDataSource2.ID is underline :)

        GridView1.DataBind()

    cheers

    Mark as me if my question or my answer can be helpful for you :)
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 8:30 AM
    • Loading...
    • vinkesh
    • Joined on 05-15-2008, 6:34 AM
    • Posts 38

    Hi,

            Sorry for incorrect code. check

    GridView1.DataSourceID = SqlDataSource2.ID

    GridView1.DataBind()

     

     

      

    Mark this post as "Answered" if it helps

    Thank You.
  • Re: searh ??? textbox1 and gridview :)

    05-15-2008, 8:41 AM
    Answer
    • Loading...
    • booler
    • Joined on 08-15-2005, 10:22 AM
    • Brighton, England
    • Posts 959

     You can't pass the text in as a string- if yuor ID is an integer, your parameter type should also be an integer:

    SglDataSource2.SelectParameters.Add("@productID", Integer.Parse(TextBox1.Text))