here is my admin.aspx.vb
Imports System.Data.SqlClient
Partial Class Admin
Inherits System.Web.UI.Page
Dim conn As New SqlConnection("Data Source=CHIRAG-PC;Initial Catalog=car;Integrated Security=True")
Dim cmd As SqlCommand
Dim drd As SqlDataReader
Dim adp As SqlDataAdapter
Dim y As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim x As Integer
x = GridView1.SelectedIndex
y = GridView1.Rows(x).Cells(1).Text
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
str = "update carHeader set cartype='" + car.Text.ToString() + "',imagefile='" + img.Text + "',capacity=" + cap.Text + "where id=" + Convert.ToDouble(y)
conn.Open()
cmd = New SqlCommand(str, conn)
cmd.ExecuteNonQuery()
conn.Close()
End Sub
End Class
and its admin.aspx
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Admin.aspx.vb" Inherits="Admin" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<br />
<br />
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="MainContent">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=CHIRAG-PC;Initial Catalog=car;Integrated Security=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [carHeader] ORDER BY [id]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Height="149px" Width="267px">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id"
SortExpression="id" />
<asp:BoundField DataField="cartype" HeaderText="cartype"
SortExpression="cartype" />
<asp:BoundField DataField="imagefile" HeaderText="imagefile"
SortExpression="imagefile" />
<asp:BoundField DataField="capacity" HeaderText="capacity"
SortExpression="capacity" />
</Columns>
</asp:GridView>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Image file"></asp:Label>
<asp:TextBox ID="img" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Car Type"></asp:Label>
<asp:TextBox ID="car" runat="server"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="capacity"></asp:Label>
<asp:TextBox ID="cap" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Save" />
</asp:Panel>
</asp:Content>
getting an error in sql query regarding its update statement
and table contents are of carHeader:
cartype varchar ,id int,imagefile varchar,capacity int
... + cap.Text + "where ...
Notice the lack of a space before where. It's probably producing a statement like:
... field = valuewhere ...
This would break the SQL.
Additionally, it's bad practice to use string concatenation in SQL statements like this, both from a security and performance point of view. You'll want to look into using parameterized queries.
Edit: Based on your comment here:
its givin me error conversion from update set cartype=....to double type is not valid
It sounds like the error is referring to this part of the query:
set cartype='" + car.Text.ToString() + "'
What type is cartype? According to the error message, it's a double. But according to your query, you're trying to set it to a string value (by wrapping it in single quotes). If it's a double then it needs to be a numeric value, not a string.
Related
struggling since morning. the scenario is as follow:
I have a ASP.NET web page having
two dropdownlist
two buttons
One GridView
Wants to fill gridview based on dropdownlist selected value
here is the .aspx code
<%# Page Title="" Language="VB" MasterPageFile="~/Admin/ADMIN.master" AutoEventWireup="false" CodeFile="EditSTUdetail.aspx.vb" Inherits="Admin_EditSTUdetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
.auto-style1
{
height: 42px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div align="center">
<fieldset>
<legend style="font-size: 16px;">SEARCH / EDIT / DELETE STUDENT RECORDS
</legend>
<div style="overflow: auto">
<div>
<asp:Label ID="lblMsg" runat="server" CssClass="lblresponse" />
<table style="margin: 3px auto 1px auto; height: 72px;">
<tr>
<td style="text-align: right;" class="auto-style2">Select Session/सत्र का चयन करें :
</td>
<td class="auto-style3">
<asp:DropDownList ID="ddlSession" runat="server" AppendDataBoundItems="True" Width="236px" Height="28px">
<asp:ListItem Text="--Select Session--" Value=""></asp:ListItem>
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="ddlSession" ForeColor="#990000"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="text-align: right;" class="auto-style1">Select Course /कक्षा का चयन करें :
</td>
<td>
<asp:DropDownList ID="ddlCourse" runat="server" AppendDataBoundItems="true" Width="236px" Height="28px">
<asp:ListItem Text="--Select Course--" Value=""></asp:ListItem>
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="ddlCourse" ForeColor="#990000"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td class="auto-style1"></td>
<td class="auto-style1">
<asp:Button ID="btnSearch" runat="server" Text="SEARCH" CssClass="button" OnClick="btnSearch_Click" Height="34px" Width="77px" /></td>
<td colspan="2" class="auto-style1">
<asp:Button ID="btnRefresh" runat="server" Text="REFRESH" CssClass="button" OnClick="btnRefresh_Click" Height="34px" Width="77px" /></td>
</tr>
</table>
</div>
</div>
<asp:GridView ID="GVdata" runat="server" Width="674px" CaptionAlign="Top"
AutoGenerateColumns="False" Height="100px" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" GridLines="Vertical"
EmptyDataText="There Are No Record Found" OnRowCancelingEdit="gvManageOrders_RowCancelingEdit"
OnRowDeleting="gvManageOrders_RowDeleting" OnRowEditing="gvManageOrders_RowEditing"
OnRowUpdating="gvManageOrders_RowUpdating">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="Sr. No." ItemStyle-Width="20">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit/Remove" ShowDeleteButton="True"
ShowEditButton="True" DeleteText="Remove" />
<asp:TemplateField HeaderText="Student ID" Visible="True">
<ItemTemplate>
<asp:Label ID="studentID" runat="server" Text='<%# Bind("studentID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name" Visible="True">
<ItemTemplate>
<asp:Label ID="Sname" runat="server" Text='<%# Bind("Sname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Father's Name" Visible="True">
<ItemTemplate>
<asp:Label ID="Fname" runat="server" Text='<%# Bind("Fname")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle BackColor="#eeeeee" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px" Font-Size="Large" ForeColor="#851010"
HorizontalAlign="Center" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#851010" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#851010" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</fieldset>
</div>
</asp:Content>
and here is my vb code:
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Net.Mail
Imports System.Data
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Partial Public Class Admin_EditSTUdetail
Inherits System.Web.UI.Page
Private myds As DataSet
Protected Sub LoadSession()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
con.Open()
Dim com As New SqlCommand("select sessionID, session from tblcategories ORDER by session DESC", con)
Dim da As New SqlDataAdapter(com)
Dim ds As New DataSet()
da.Fill(ds)
ddlSession.DataTextField = ds.Tables(0).Columns("session").ToString()
' text field name of table dispalyed in dropdown
ddlSession.DataValueField = ds.Tables(0).Columns("sessionID").ToString()
' to retrive specific textfield name
ddlSession.DataSource = ds.Tables(0)
'assigning datasource to the dropdownlist
ddlSession.DataBind()
ddlSession.SelectedIndex = -1
'binding dropdownlist
End Sub
Protected Sub LoadCourse()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
con.Open()
Dim com As New SqlCommand("SELECT DISTINCT course AS mycolumn FROM " _
& " tblsubjects where course is not null and " _
& "not course = '' order by mycolumn", con)
Dim da As New SqlDataAdapter(com)
Dim ds As New DataSet()
da.Fill(ds)
ddlCourse.DataTextField = ds.Tables(0).Columns("mycolumn").ToString()
' text field name of table dispalyed in dropdown
ddlCourse.DataValueField = ds.Tables(0).Columns("mycolumn").ToString()
' to retrive specific textfield name
ddlCourse.DataSource = ds.Tables(0)
'assigning datasource to the dropdownlist
ddlCourse.DataBind()
ddlCourse.SelectedIndex = -1
'binding dropdownlist
End Sub
Protected Sub fillgrid()
Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("cmsDB").ConnectionString)
Dim cmd As New SqlCommand("SELECT StudentID,session,course,Sname,Fname FROM [tblstudetail] WHERE " _
& " [session] = '" + ddlSession.SelectedValue.ToString() + "' AND " _
& " [course] = '" + ddlCourse.SelectedValue.ToString() + "'", cn)
cn.Open()
Dim da As New SqlDataAdapter(cmd)
myds = New DataSet()
da.Fill(myds)
GVdata.DataSource = myds
GVdata.DataBind()
End Sub
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
LoadSession()
LoadCourse()
fillgrid()
End If
End Sub
Protected Sub gvManageOrders_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
Throw New NotImplementedException
End Sub
Protected Sub gvManageOrders_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Throw New NotImplementedException
End Sub
Protected Sub gvManageOrders_RowEditing(sender As Object, e As GridViewEditEventArgs)
Throw New NotImplementedException
End Sub
Protected Sub gvManageOrders_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Throw New NotImplementedException
End Sub
Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
If Not Page.IsPostBack Then
fillgrid()
End If
End Sub
Protected Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
GVdata.DataSource = Nothing
End Sub
End Class
But it is not returning what i request.
Kindly HELP me out this....
PLease remove these lines ,
ddlSession.SelectedIndex = -1
ddlCourse.SelectedIndex = -1
from the end of the LoadCourse and LoadSession methods. These are resetting the dropdown values and causing issue.
The flow is messed up in your code. You are calling loadsession , loadcourses and fillgrid serially from the page load method. Think about it. When the code in fillgrid executes there is no selected value in the drop downs. Obviously no data is found and so the gridview is not visible.
The better way to handle is introduce two new methods like this,
Protected Sub ddlCourses_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlSession.SelectedIndexChanged
and
Protected Sub ddlAC3_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCourse.SelectedIndexChanged
Inside these methods send out a call to fillgrid.
Also inside the fillgrid method first validate there is some value in both the ddlCourse and ddlSession dropdowns , then send out the database command.
First of all
ddlSession.SelectedValue.ToString()
become
ddlSession.SelectedValue
Then in Protected Sub btnSearch_Click remove
If Not Page.IsPostBack Then
Then
ddlCourse.SelectedIndex = -1
become
ddlCourse.ClearSelection();
Finally, are you sure there aren't NULL value in the database?
I have a Gridview which is populating with entries fine.
What i need to do is enable it, so that when a string is entered within the textbox, it changes the SQLDatasource parameter to search for entries that match.
So this is the ASPx file that is being used.
<%# Page Language="vb" AutoEventWireup="True" CodeBehind="test.aspx.vb" MasterPageFile="~/Site1.Master"%>
<asp:Content ID="content" runat="server" ContentPlaceHolderID="Content">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<span class="country"></span>
<div align="right">
<asp:TextBox runat="server" ID="textbox1"></asp:TextBox>
<asp:Button runat="server" Text="Search" ID="Button1" OnClientClick="Button1_click" PostBackUrl="~/test.aspx" />
</div>
<br />
<br />
<div align="center">
<asp:UpdatePanel runat="server" ID="updatepanel">
<ContentTemplate>
<asp:SqlDataSource runat="server" ID="datab" SelectCommand="SELECT * FROM [TEST] where [unit]=#unit" ConnectionString="<%$ ConnectionStrings:Con %>" ProviderName="<%$ ConnectionStrings:Con.ProviderName %>">
<SelectParameters>
<asp:Parameter DefaultValue="UK" Name="unit" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="View" DataSourceID="datab" runat="server" CssClass="table" AllowSorting="True" AutoGenerateColumns="True" DataKeyNames="Id">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
The above works absolutley fine, I can change the parameter manually, and it will work. However i cannot get it to update from user submission.
Backfile:
Public Class test
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim unit As String = textbox1.Text
datab.UpdateParameters("Unit").DefaultValue = unit
updatepanel.Update()
End Sub
End Class
I'm guessing that i am doing something wrong when referrencing the code behind page, as the page seems to refresh when i press the button.
Any help is appreciated.
******Edit********
I have tried to use the following code in the behind file, however it does not display anything on the actual page now.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim DS As SqlDataSource = New SqlDataSource
Dim con As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\local\test.accdb"
Dim sc As String = "SELECT * FROM [TEST] where [unit]=#unit"
DS.ID = "datab"
DS.ConnectionString = con
DS.SelectCommand = sc
DS.SelectParameters("Unit").DefaultValue = "UK"
View.DataSource = datab
View.DataBind()
End Sub
I am developing a webpage that will utilize two cascading drop down lists and an update panel that contains a nested gridview.
I have managed to get the cascading drop down lists to work correctly, and I have also managed to get my nested gridview working.
The next step is to populate the gridview based on the selection of the second cascading drop down list, which is where the update panel comes into play. I figured that once a selection has been made to the second drop down list, I could then refresh the update panel and load my nested gridviews. Looking through the documentation, I realized that I needed a trigger of some sort to force the update panel to reload, but I must not be doing something correctly...
Within my update panel, I use the trigger parameter, where cddMachine is the name of the second drop down list.
<Triggers>
<asp:AsyncPortBackTrigger ControlID="cddMachine" EventName="SelectedIndexChanged" />
</Triggers>
Upon running my code, the following error is thrown:
Control with ID 'cddMachine' being registered through
RegisterAsyncPostBackControl or RegisterPostBackControl must implement either
INamingContainer, IPostBackDataHandler, or IPostBackEventHandler
After searching for a while, I have been unable to find any clear information as to how one would go about refreshing an update panel by means of a cascading drop down list. I'm sure I'm overlooking something simple, but I am not sure what it is that I need to look for.
EDIT - Included code
lockout.aspx
<%# Page Title="" Language="vb" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="Lockout.aspx.vb" Inherits="LockoutTagout.Lockout" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="asm" runat="server" />
<div>
Complex:
<asp:DropDownList ID="ComplexList" runat="server" />
Machine:
<asp:DropDownList ID="MachineList" runat="server" AutoPostBack="true" />
<asp:CascadingDropDown ID="cddComplex" runat="server"
ServicePath="~/lockoutService.asmx" ServiceMethod="GetComplex"
TargetControlID="ComplexList" Category="Complex"
PromptText="Select Complex" />
<asp:CascadingDropDown ID="cddMachine" runat="server"
ServicePath="~/lockoutService.asmx" ServiceMethod="GetMachine"
TargetControlID="MachineList" ParentControlID="ComplexList"
Category="Machine" PromptText="Select Machine" />
</div>
<div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>"
SelectCommand="SELECT * FROM [DEVICES] WHERE machine_id = #machine_id">
<SelectParameters>
<asp:ControlParameter Name="machine_id" ControlID="cddMachine" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" GridLines="None" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="devices_id" DataSourceID="SqlDataSource1" AlternatingRowStyle-BackColor="White">
<Columns>
<asp:BoundField DataField="devices_id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="devices_id" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="description" HeaderText="Description" SortExpression="description" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="location" HeaderText="Location" SortExpression="location" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="verification" HeaderText="Verification" SortExpression="verification" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="rack_num" HeaderText="Rack_#" SortExpression="rack_num" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="section_num" HeaderText="Section_#" SortExpression="section_num" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="color_or_number" HeaderText="Key_Identifier" SortExpression="color_or_number" />
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="normal_position" HeaderText="Normal_Position" SortExpression="normal_position" />
<asp:TemplateField HeaderText="P&P Numbers" ControlStyle-Width="100px">
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>" SelectCommand="SELECT * FROM [PNP] WHERE ([devices_id] = #devices_id)">
<SelectParameters>
<asp:Parameter Name="devices_id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" ShowHeader="false" GridLines="None" runat="server" AutoGenerateColumns="False" DataKeyNames="pnp_id" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="pnp_num" HeaderText="pnp_num" SortExpression="pnp_num" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#a52138" ForeColor="White" Font-Size="Medium" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
lockout.aspx.vb
Public Class Lockout
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, MachineList.SelectedIndexChanged
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim s As SqlDataSource = CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
s.SelectParameters(0).DefaultValue = e.Row.Cells(0).Text
End If
End Sub
End Class
lockoutService.asmx.vb
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports AjaxControlToolkit
Imports System.Data.SqlClient
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class lockoutService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetComplex(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim conn As New SqlConnection("Data Source=WIWRMS-SQLD2\SQLD2;Initial Catalog=LOTO;Persist Security Info=True;User ID=LOTO_ADMIN;Password=lotoadmin")
conn.Open()
Dim comm As New SqlCommand( _
"SELECT * FROM complex", conn)
Dim dr As SqlDataReader = comm.ExecuteReader()
Dim l As New List(Of CascadingDropDownNameValue)
While (dr.Read())
l.Add(New CascadingDropDownNameValue(dr("complex_name").ToString(), dr("complex_id").ToString()))
End While
conn.Close()
Return l.ToArray()
End Function
<WebMethod()> _
Public Function GetMachine(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim complex_id As Integer
Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
If Not kv.ContainsKey("complex") Or Not Int32.TryParse(kv("complex"), complex_id) Then
Throw New ArgumentException("Couldn't find complex.")
End If
Dim conn As New SqlConnection("Data Source=WIWRMS-SQLD2\SQLD2;Initial Catalog=LOTO;Persist Security Info=True;User ID=LOTO_ADMIN;Password=lotoadmin")
conn.Open()
Dim comm As New SqlCommand( _
"SELECT * FROM machine WHERE complex_id = #complex_id", conn)
comm.Parameters.AddWithValue("#complex_id", complex_id)
Dim dr As SqlDataReader = comm.ExecuteReader()
Dim l As New List(Of CascadingDropDownNameValue)
While (dr.Read())
l.Add(New CascadingDropDownNameValue(dr("machine_name").ToString(), dr("machine_id").ToString()))
End While
conn.Close()
Return l.ToArray()
End Function
End Class
A CascadingDropDown is really just an extender on a DropDownList, so you should be able to remove the trigger, set your final DropDownList's AutoPostBack property to true, then handle its SelectedIndexChanged event. That should automatically trigger the PartialPostBack so that you can update your GridView accordingly.
Additional details for fix:
I'm thinking you may want to change the following in your DataSource markup: <asp:ControlParameter Name="machine_id" ControlID="cddMachine" PropertyName="SelectedValue" /> change ControlID to MachineList.
net with VB.net. I wanted to sort column dgv table but the code not working.
The client side like this
<asp:GridView ID="griddata" runat="server" AutoGenerateColumns="false" AllowSorting="True" >
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name"/>
<asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
</Columns>
</asp:GridView>
The server side like this
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO
Partial Public Class Course
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
listhendle()
End Sub
Protected Sub listhendle()
Dim ds As New DataSet
Dim cls As New connections
ds = cls.returndataset("select * from [m.student] ")
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
End Class
did i miss something i got the reference from here and i change data connection
Without Codebehind :
You can do one thing.... Do the database connection #codebehind and copy following code in aspx file and rename it....
It will work...
in aspx : set allowsorting =true
in C# :
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gvEmployeeList .DataSource as DataTable;
DataTable dtEmplist = new DataTable();
dtEmplist = iz.getEmployeeList();
if (dtEmplist.Rows.Count > 0 )
{
DataView dataView = new DataView(dtEmplist);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
gvEmployeeList.DataSource = dataView;
gvEmployeeList.DataBind();
}
}
This is my datalist:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatLayout="Flow">
<ItemTemplate>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("ID") %>' Visible="False" />
Titre:
<asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("ID", "Handler.ashx?ID={0}") %>' Width="200" Height="200"/>
<br />
comments:
<asp:Label ID="commentsLabel" runat="server" Text='<%# Eval("comments") %>' />
<br />
Ajouter commentaire
<asp:button ID="btnAjouter" runat="server" OnCommand="Button_Command"
CommandName="add" Text="Ajouter" />
<asp:TextBox ID="TextBoxComments" runat="server"></asp:TextBox>
<br/>
<br/>
</ItemTemplate>
</asp:DataList>
This my aspx.vb button event:
Sub Button_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim connectionString As String = WebConfigurationManager.ConnectionStrings("BecsEtMuseauxSQL").ConnectionString
Dim con As SqlConnection = New SqlConnection(connectionString)
con.Open()
Dim cmd As New SqlCommand("updateComments", con)
cmd.CommandType = CommandType.StoredProcedure
//I try this ....
cmd.Parameters.Add("#id", SqlDbType.Int).Value = DataList1.FindControl("IdLabel").ToString()
cmd.Parameters.Add("#Comments", SqlDbType.NVarChar).Value = DataList1.FindControl("TextBoxComments").ToString()
cmd.ExecuteNonQuery()
End Sub
I get an error like this: "The object reference is not definied to an object instance"
How can I catch the value from the label IdLabel and TextBoxComments in this situation?
You should handle the DataList's ItemCommand instead of the Button's Command.
Then you can find your controls with e.Item.FindControl:
Private Sub DataList1_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.ItemCommand
If e.CommandName = "add" Then
Dim IdLabel = DirectCast(e.Item.FindControl("IdLabel"), Label)
Dim TextBoxComments = DirectCast(e.Item.FindControl("TextBoxComments"), TextBox)
' ... '
End If
End Sub
on aspx, remove your redundant Command-handler:
<asp:button ID="btnAjouter" CommandName="add" Text="Ajouter" runat="server" />
If you want to use the Button's Click-Event instead, that's possible also.
Protected Sub btnAjouter_Click(sender As Object, e As EventArgs)
Dim container = DirectCast(DirectCast(sender, Control).NamingContainer, DataListItem)
Dim IdLabel = DirectCast(container.FindControl("IdLabel"), Label)
Dim TextBoxComments = DirectCast(container.FindControl("TextBoxComments"), TextBox)
' ... '
End Sub
on aspx, add the click-event handler to the button:
<asp:button ID="btnAjouter" OnClick="btnAjouter_Click" Text="Ajouter" runat="server" />