B
Ben
Hi,
It's about a database containing (for simplifying) three fields: 'placenr'
(primary key), 'place' and 'color'. The records are updatable (Edit/Update
button) in a gridview. For updating the field 'color', the selectedvalue of
a dropdownlist must be used, containing all possible colors (red, blue ...),
so the field 'color' is ReadOnly in the gridview.
Everything works, but i still have 2 problems:
1)the new color in the field is always the first element of the dropdownlist
(here the value "choose"), whatever i choose in it.
2)If only the field 'place' must be updated but not the field 'color'(no
click in the dropdownlist), the original color in the field 'color' is
nevertheless always replaced by the first value (again value "choose") in
the dropdownlist.
Any hints or idea for solving that?
Thanks
Ben
Here part of the code-behind file:
----------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
...
sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
= c:\mydb.mdb"
sql = "SELECT [color] FROM [color];"
d = New OleDbDataAdapter(sql, sConnectionString)
ds = New DataSet()
x = d.Fill(ds)
If Page.IsPostBack Then
dindex = DropDownList1.SelectedIndex
dvalue = DropDownList1.SelectedValue
DropDownList1.Items.Clear()
End If
z = New ListItem("choose a color", "choose")
DropDownList1.Items.Add(z)
For i = 0 To x - 1
kl = ds.Tables(0).Rows(i).Item(0)
z = New ListItem(kl, kl)
DropDownList1.Items.Add(z)
Next
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If dindex > 0 Then '0 = "choose"
DropDownList1.SelectedValue = dvalue
End If
End Sub
End Class
Here part of the aspx file:
---------------------------
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydb.mdb"
OldValuesParameterFormatString="original_{0}"
ProviderName="System.Data.OleDb"
SelectCommand="SELECT * FROM [place]"
UpdateCommand="UPDATE [place] SET [placename] = ?, [color] = @color WHERE
[placenr] = ?">
<UpdateParameters>
<asparameter Name="placename" Type="String" />
<asp:ControlParameter ControlID="dropdownlist1"
Name="color" PropertyName="SelectedValue" Type="String" />
<asparameter Name="original_placenr" Type="Int32" />
<asparameter Name="original_placename" Type="String" />
<asparameter Name="original_color" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="placenr" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True"/>
<asp:BoundField DataField="placenr" ReadOnly="True"/>
<asp:BoundField DataField="placename" />
<asp:BoundField DataField="color" ReadOnly="True" />
</Columns>
</asp:GridView>
<aspropDownList ID="DropDownList1"
runat="server" AutoPostBack="True">
</aspropDownList>
It's about a database containing (for simplifying) three fields: 'placenr'
(primary key), 'place' and 'color'. The records are updatable (Edit/Update
button) in a gridview. For updating the field 'color', the selectedvalue of
a dropdownlist must be used, containing all possible colors (red, blue ...),
so the field 'color' is ReadOnly in the gridview.
Everything works, but i still have 2 problems:
1)the new color in the field is always the first element of the dropdownlist
(here the value "choose"), whatever i choose in it.
2)If only the field 'place' must be updated but not the field 'color'(no
click in the dropdownlist), the original color in the field 'color' is
nevertheless always replaced by the first value (again value "choose") in
the dropdownlist.
Any hints or idea for solving that?
Thanks
Ben
Here part of the code-behind file:
----------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
...
sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
= c:\mydb.mdb"
sql = "SELECT [color] FROM [color];"
d = New OleDbDataAdapter(sql, sConnectionString)
ds = New DataSet()
x = d.Fill(ds)
If Page.IsPostBack Then
dindex = DropDownList1.SelectedIndex
dvalue = DropDownList1.SelectedValue
DropDownList1.Items.Clear()
End If
z = New ListItem("choose a color", "choose")
DropDownList1.Items.Add(z)
For i = 0 To x - 1
kl = ds.Tables(0).Rows(i).Item(0)
z = New ListItem(kl, kl)
DropDownList1.Items.Add(z)
Next
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If dindex > 0 Then '0 = "choose"
DropDownList1.SelectedValue = dvalue
End If
End Sub
End Class
Here part of the aspx file:
---------------------------
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydb.mdb"
OldValuesParameterFormatString="original_{0}"
ProviderName="System.Data.OleDb"
SelectCommand="SELECT * FROM [place]"
UpdateCommand="UPDATE [place] SET [placename] = ?, [color] = @color WHERE
[placenr] = ?">
<UpdateParameters>
<asparameter Name="placename" Type="String" />
<asp:ControlParameter ControlID="dropdownlist1"
Name="color" PropertyName="SelectedValue" Type="String" />
<asparameter Name="original_placenr" Type="Int32" />
<asparameter Name="original_placename" Type="String" />
<asparameter Name="original_color" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="placenr" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True"/>
<asp:BoundField DataField="placenr" ReadOnly="True"/>
<asp:BoundField DataField="placename" />
<asp:BoundField DataField="color" ReadOnly="True" />
</Columns>
</asp:GridView>
<aspropDownList ID="DropDownList1"
runat="server" AutoPostBack="True">
</aspropDownList>