Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将Url绑定到Gridview_Asp.net_Vb.net_Odbc - Fatal编程技术网

Asp.net 将Url绑定到Gridview

Asp.net 将Url绑定到Gridview,asp.net,vb.net,odbc,Asp.net,Vb.net,Odbc,我想将url绑定到GridView,但我不知道如何绑定 例如,当我键入http://localhost:12345/example.aspx?FirstName=John在url中,它将在GridView中给出结果,该结果仅以名字“John”显示 这是我目前的代码: Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim dt As New DataTable ' Stablis

我想将
url
绑定到
GridView
,但我不知道如何绑定

例如,当我键入
http://localhost:12345/example.aspx?FirstName=John
url
中,它将在
GridView
中给出结果,该结果仅以名字“John”显示

这是我目前的代码:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim dt As New DataTable
    ' Stablish ODBC Connection
    Dim con As New OdbcConnection("DRIVER={SQL Server};Server=WJNJPHR8TCX8P\SQLEXPRESS;Database=Fabrics;Integrated Security=True;")
    ' Query Command
    Dim cmd As New OdbcCommand("SELECT * FROM [Client] WHERE [FirstName] = ?", con)
    con.Open()

    ' Gets the path (Example.aspx)
    Dim path As String = HttpContext.Current.Request.Url.AbsolutePath
    ' Gets the host (localhost)
    Dim host As String = HttpContext.Current.Request.Url.Host
    ' Gets the whole url (localhost:24124/Example.aspx)
    Dim url As String = HttpContext.Current.Request.Url.AbsoluteUri
    ' Parse the query string variables into a NameValueCollection
    Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(url)

    ' Iterate through the collection and shows the result in MsgBox
    Dim sb As New StringBuilder()
    For Each s As String In qscoll.AllKeys
        sb.Append(s & " = " & qscoll(s) & vbCrLf)
    Next s
    MsgBox(sb.ToString)

    ' Gets all keys and values in query string and shows it on MsgBox
    For Each key As String In HttpContext.Current.Request.QueryString.AllKeys
        MsgBox("Key: " + key + "  Value: " + Request.QueryString(key))
    Next key

    Dim FName As String = Request.QueryString("FirstName")
    Dim par1 As New OdbcParameter
    par1.OdbcType = OdbcType.NVarChar
    par1.Value = FName
    par1.ParameterName = "@FirtName"
    cmd.Parameters.Add(par1)


    'Shows the result in Data Grid
    dt.Load(cmd.ExecuteReader()) '==> Error: Invalid use of default parameter
    GridView1.DataSource = dt
    GridView1.DataBind()

End Sub

任何帮助都可以

您可以使用直接访问查询字符串

Dim firstName as string = Request.QueryString("firstName")
然后必须在执行它之前将其作为查询的参数传递

Dim par1 As New OdbcParameter
par1.DbType = DbType.String
par1.Value = firstName
par1.ParameterName = "@FirstName"
cmd.Parameters(1) = par1
希望对你有所帮助

回答了我的问题

这是我的密码:

    For Each key As String In HttpContext.Current.Request.QueryString.AllKeys
        Dim FName As String = Request.QueryString("FirstName")
        Dim par1 As New OdbcParameter With {
            .OdbcType = OdbcType.NVarChar,
            .Value = FName,
            .ParameterName = "@FirstName"
        }
        cmd.Parameters.Add(par1)

        MsgBox("Key: " + key + "  Value: " + Request.QueryString(key))
        dt.Load(cmd.ExecuteReader())
        GridView1.DataSource = dt
        GridView1.DataBind()
    Next key

@谢谢你的帮助!:)

谢谢大家!@isol我将尝试:)我尝试了您的代码,它给我一个错误,上面写着“计数为0的此OdbcParameterCollection的索引1无效”。我试图编辑您的代码,但它给了我相同的错误。我试图编辑
par1.DbType=DbType.String
par1.OdbcType=OdbcType.NVarChar
对不起,尝试
cmd.Parameters.Add(par1)
我也尝试过,并且给我一个错误,上面写着“参数使用无效”谢谢你的帮助也许我会自己调试:)@isol