'The ConnectionString property has not been initialized' won't go away - asp.net

I've been getting this error for over a day now and can't afford to waste time when my deadline is tight. Similar code works and I can't see the difference and why this code is giving this error. When I try to update the changes I edit on my gridview the error occurs.
my markup:
<asp:GridView ID="diary" runat="server" AutoGenerateColumns="False" DataSourceID="sDiary"
DataKeyNames="DiaryId" EnableViewState="False">
<Columns>
<asp:BoundField DataField="DiaryId" HeaderText="DiaryId" InsertVisible="False" ReadOnly="True"
SortExpression="DiaryId" Visible="False" />
<asp:BoundField DataField="DiaryItem" HeaderText="DiaryItem" SortExpression="DiaryItem">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="DiaryDate" HeaderText="DiaryDate" SortExpression="DiaryDate">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sDiary" runat="server" SelectCommand="SELECT DiaryItem, left(convert(varchar,DiaryDate,120),10) as DiaryDate, DiaryId FROM a_Diary WHERE CustomerId=#CustomerId And ProcessedDate is null ORDER BY DiaryDate"
UpdateCommand="UPDATE a_Diary SET DiaryItem=#DiaryItem, DiaryDate=#DiaryDate WHERE DiaryId=#DiaryId">
<SelectParameters>
<asp:Parameter Name="CustomerId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DiaryId" Type="Int32" />
<asp:Parameter Name="DiaryItem" Type="String" />
<asp:Parameter Name="DiaryDate" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
sDiary.ConnectionString = ConfigurationManager.ConnectionStrings["BZSSSqlConnectionString"].ConnectionString;
sDiary.SelectParameters["CustomerId"].DefaultValue = Request.QueryString["c"];
}
Can anyone help me?

Related

Dropdown List issue in ASP.net gridview edit mode

I have a asp.net gridview that is giving me fits. I'm pulling the data into the grid with no issues. However, when I click edit, I get this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'wrkCode'.
My intention is to allow the user to edit a group's work schedule using a dropdown list. Here is my code:
<asp:GridView ID="grdShowGroups" runat="server" datakeynames="grpID" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Vertical" CellSpacing="5" Width="700px" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="grdShowGroups_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit" />
<asp:BoundField DataField="grpID" HeaderText="grpID" SortExpression="grpID" InsertVisible="False" ReadOnly="True" Visible="false" />
<asp:BoundField DataField="grpStartTime" HeaderText="Start Time" SortExpression="grpStartTime" />
<asp:BoundField DataField="grpEndTime" HeaderText="End Time" SortExpression="grpEndTime" />
<asp:TemplateField HeaderText="Work Schedule" SortExpression="wrkSchedule">
<EditItemTemplate>
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>' AppendDataBoundItems="true" AutoPostBack="true"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblWrkSchedule" runat="server" Text='<%# Bind("wrkSchedule") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="grpDescription" HeaderText="Description" SortExpression="grpDescription" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [grpID], [grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription] FROM [empGroups]" DeleteCommand="DELETE FROM [empGroups] WHERE [grpID] = #grpID" InsertCommand="INSERT INTO [empGroups] ([grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription]) VALUES (#grpStartTime, #grpEndTime, #wrkSchedule, #grpDescription)" UpdateCommand="UPDATE [empGroups] SET [grpStartTime] = #grpStartTime, [grpEndTime] = #grpEndTime, [wrkSchedule] = #wrkSchedule, [grpDescription] = #grpDescription WHERE [grpID] = #grpID">
<DeleteParameters>
<asp:Parameter Name="grpID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
<asp:Parameter Name="grpID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [wrkCode], [wrkDescription], [wrkID] FROM [wrkSchedule]"></asp:SqlDataSource>
If I run it with this code for the dropdown list:
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>'>
I get the error. IF I run it without the "SelectedValue='<%#Bind("wrkCode")%>'" code, it presents the dropdown list as I need, just without a selected value that connects to the data from the data currently in the table.
I've looked at several examples from all over the web and it may be that I'm just missing something small. I just can't figure this out.
Thanks!
You need to select the wrkCode in your sqldatasource1 otherwise it has nothing to bind to. So just include wrkCode in your select clause of that sql statement and you should be good to go.

incorrect syntax near nvarchar in gridview

I have a problem about the updating a column of my gridview; when I try to update the field I get this error:
Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "#pid".
Here is a piece of my code:
<asp:GridView ID="GridView1" runat="server" name="gd" Height="100px"
Width="435px" AutoGenerateColumns="False" DataKeyNames="pid" DataSourceID="SqlDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="pid" HeaderText="pid"
SortExpression="pid" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="hoghoghe rozane" HeaderText="hoghoghe rozane"
SortExpression="hoghoghe rozane" />
<asp:BoundField DataField="paye" HeaderText="paye"
SortExpression="paye" />
<\Columns>
<asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
UpdateCommand="UPDATE [post] SET [name] = #name, hoghoghe_rozane = #hoghoghe_rozane, [paye] = #paye WHERE (pid = #pid)" >
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="hoghoghe_rozane" Type="Int32" />
<asp:Parameter Name="paye" Type="Int32" />
<asp:Parameter Name="pid" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
chek this out : same problem
it says you must change [hoghoghe rozane] into [hoghoghe_rozane]
Update :::
1.Make sure that the type of pid field in database
2.fields should not have spaces in your tables
3.alias should not have spaces too
try this code :
<asp:GridView ID="GridView1" runat="server" name="gd" Height="100px"
Width="435px" AutoGenerateColumns="False" DataKeyNames="pid" DataSourceID="SqlDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="pid" HeaderText="pid"
SortExpression="pid" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="hoghoghe_rozane" HeaderText="hoghogheRozane"
SortExpression="hoghoghe_rozane" />
<asp:BoundField DataField="paye" HeaderText="paye"
SortExpression="paye" />
<\Columns>
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
UpdateCommand="UPDATE [post] SET [name] = #name, hoghoghe_rozane = #hoghoghe_rozane, [paye] = #paye WHERE (pid = #pid)" >
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="hoghoghe_rozane" Type="Int32" />
<asp:Parameter Name="paye" Type="Int32" />
<asp:Parameter Name="pid" Type="String" />
</UpdateParameters>
hope that would work.

Can you please tell me why I am getting `The GridView 'GridView1' fired event RowUpdating which wasn't handled.` error message

I have been able to work through the initial problem of not being able to insert records consistently but a new error message crops up.
Now, I am getting The GridView 'GridView1' fired event RowUpdating which wasn't handled.
This error occurs when I click the Update button to update a row of record.
First, I click Edit button. This exposes the Update/Cancel buttons.
When I click the Update button, I get the aforementioned error.
My first take was that gridview needed onRowUpdating but after adding (see markup code), still getting same error.
This is a bit frustrating.
Here is the code.
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dd As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlstatus"), DropDownList)
e.NewValues("status") = dd.SelectedItem.Text
End Sub
Markup:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="reqnum" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None" Visible="True"
OnRowDataBound="gvRowDataBound" OnRowEditing="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
EnableViewState="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="rownum" HeaderText="rownum" InsertVisible="False" ReadOnly="True"
SortExpression="rownum" />
<asp:BoundField DataField="reqnum" HeaderText="reqnum" SortExpression="reqnum" />
<asp:BoundField DataField="reqrecdate" HeaderText="reqrecdate" SortExpression="reqrecdate" />
<asp:BoundField DataField="reqrecfrom" HeaderText="reqrecfrom" SortExpression="reqrecfrom" />
<asp:BoundField DataField="skillsets" HeaderText="skillsets" SortExpression="skillsets" />
<asp:BoundField DataField="application" HeaderText="application" SortExpression="application" />
<asp:BoundField DataField="hoursperweek" HeaderText="hoursperweek" SortExpression="hoursperweek" />
<asp:BoundField DataField="fromdate" HeaderText="fromdate" SortExpression="fromdate" />
<asp:BoundField DataField="todate" HeaderText="todate" SortExpression="todate" />
<%-- <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />--%>
<asp:TemplateField HeaderText="status">
<EditItemTemplate>
<asp:DropDownList id="ddlstatus" CssClass="dropdown" DataSourceID="DSforDDL" runat="server">
<asp:ListItem Text="none" Value=""></asp:ListItem>
<asp:ListItem>Not Started</asp:ListItem>
<asp:ListItem>Pending</asp:ListItem>
<asp:ListItem>Completed</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<% #Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="statusupdate" HeaderText="statusupdate" SortExpression="statusupdate" />
<asp:BoundField DataField="statusupby" HeaderText="statusupby" SortExpression="statusupby" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Distinct [rownum],[reqnum], [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [statusupby] FROM [Term] ORDER BY [reqnum]"
UpdateCommand="INSERT INTO Term(reum, reqrecdate, reqrecfrom, skillsets, application, hoursperweek, fromdate, todate, status, statusupby, statusupdate) VALUES (#reum,#reqrecdatelbltxt, #reqrecfromlbltxt, #skillsets, #application, #hoursperweek,#fromdate,#todate, #status, #lognametxt, #logdatetxt)"
<DeleteParameters>
<asp:Parameter Name="rownum" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="reqnum" Type="String" />
<asp:Parameter DbType="DateTime" Name="reqrecdate" />
<asp:Parameter Name="reqrecfrom" Type="String" />
<asp:Parameter Name="skillsets" Type="String" />
<asp:Parameter Name="application" Type="String" />
<asp:Parameter Name="hoursperweek" Type="Int32" />
<asp:Parameter DbType="DateTime" Name="fromdate" />
<asp:Parameter DbType="DateTime" Name="todate" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter DbType="DateTime" Name="statusupdate" />
<asp:Parameter Name="statusupby" Type="String" />
<asp:Parameter Name="rownum" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Thanks a lot in advance for your assistance.
The gridView tracks handlers to ensure something has been added to deal with the update intentionally.
Unless you are Manually calling "AddHandler" somewhwere else in code, I don't see a handles claus on your event method that would intercept the event being raised.

Client Deletion Confirmation

I have the "Delete" option showing on my GridView that is populated via SQL Data Source. I am trying to implement a way where the user will be asked to verify that they wish to delete the row of information. I have been following the guide from http://msdn.microsoft.com/en-us/library/ms972940.aspx.
The issue I'm currently having is when I add my ObjectDataSource, I select the class and when I go to choose the desired method it is not listed (Fig. 37).
Addition information - I originally created the GridView to be populated via SQLDataSource, now I'm trying to transition over to ObjectDataSource and add my DeleteMethod. I am stuck at this part and do not yet have the popup window that will ask the user to continue. I will work on that after I overcome the current challenge.
Here is my aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentAdmin" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentAdmin" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="CourseSection_ID" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID" HeaderText="CourseSection_ID"
InsertVisible="False" ReadOnly="True" SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section" HeaderText="section"
SortExpression="section" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MGT598DBConnectionString1 %>"
SelectCommand="SELECT * FROM [Course_Section]"
DeleteCommand="DELETE FROM [Course_Section] WHERE [CourseSection_ID] = #CourseSection_ID"
InsertCommand="INSERT INTO [Course_Section] ([section]) VALUES (#section)"
UpdateCommand="UPDATE [Course_Section] SET [section] = #section WHERE [CourseSection_ID] = #CourseSection_ID">
<DeleteParameters>
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="section" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="section" Type="String" />
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource> </asp:Content>
My code behind file:
namespace MGT598GraduateProject.View
{
public partial class ViewCourse_Section : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//http://msdn.microsoft.com/en-us/library/ms972940.aspx
}
public static void DeleteMethod(int CourseSection_ID)
{
// deletes a specified Order Details record
// from the Northwind Products table
string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "#OrderID";
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MGT598DBConnectionString1"].ConnectionString))
{
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myCommand.Parameters.Add(new SqlParameter("#CourseSection_ID", CourseSection_ID));
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}
There are two issues here.
First, you have a few fundamental basics wrong.
You do not need an SQLDataSource AND an ObjectDataSource.
As you have pointed the Gridview to the SqldataSource (DataSourceID="SQLDataSource1") the object data source is infact useless.
You can delete the following code from the aspx:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
<asp:ObjectDataSource> </asp:Content>
And delete the entire DeleteMethod from the code behind.
If you read the tutorial you linked us to you will see that they are two seperate sections and not to be done together.
Second, you need to have the user 'verify' the deletion.
To do this, modify your block to match the following:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat="server"
OnClientClick="return confirm('Are you sure you want to delete this record?');"
CommandName="Delete">Delete Item
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID"
HeaderText="CourseSection_ID"
InsertVisible="False"
ReadOnly="True"
SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section"
HeaderText="section"
SortExpression="section" />
</Columns>

How to use GridView edit functionality to modify a field that relies on another field

Sorry for the convoluted title, but that is the best way I could think to phrase it.
Basically, I have a column of data that is a URL (lets call it URL-A), and another that is hidden which is also a URL(URL-B). I have a function called ConvertURL(string URLin) in the codebehind that will take URL-A and turn it into URL-B.
So in this gridview, if someone were to edit an entry using the native gridview/datasource functionality and change URL-A, how do I get it to run this new URL-A through ConvertURL() and update the URL-B field?
Here is a simple representation of the code (not the actual code):
Code-behind function:
public string ConvertURL(string URLin)
{
Int32 iLocation = URLin.IndexOf("WWW.XYZ.COM");
if (iLocation >= 0)
{
string checkurl = URLin.Replace("WWW.XYZ.COM", "STAGING.XYZ.COM");
return checkurl;
}
else
{
return URLin;
}
}
Data-Source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:My_AppConnectionString %>"
SelectCommand="SELECT [URL_A], [URL_B], [URLName] FROM [My_App] WHERE ([IsActive] = #IsActive)"
OldValuesParameterFormatString="original_{0}"
UpdateCommand="UPDATE [My_App] SET [URL_A] = #URL_A, [URL_B] = #URL_B, [URLName] = #URLName, [IsActive] = #IsActive WHERE [ID] = #original_ID">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="IsActive" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="URL_A" Type="String" />
<asp:Parameter Name="URL_B" Type="String" />
<asp:Parameter Name="URLName" Type="String" />
<asp:Parameter Name="IsActive" Type="boolean" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_URL_A" Type="String" />
<asp:Parameter Name="original_URL_B" Type="String" />
<asp:Parameter Name="original_URLName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
And Lastly the GridView:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
onrowediting="GridView1_RowEditing"
AutoGenerateColumns="False" CellPadding="3" DataKeyNames="ID"
DataSourceID="SqlDataSource1" EnableModelValidation="False" ForeColor="Black"
GridLines="Vertical" AllowSorting="True" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
AutoGenerateEditButton="True" CausesValidation="false">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="URL_A" HeaderText="URL A"
SortExpression="URL_A" />
<asp:BoundField DataField="URL_B" HeaderText="URL B"
SortExpression="URL_B" **Visible="False"** />
<asp:BoundField DataField="URLName" HeaderText="URL Name"
SortExpression="URLName" />
<asp:CheckBoxField DataField="IsActive" HeaderText="Active"
SortExpression="IsActive" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
So to summarize: A user will see a gridview with 3 fields to edit, one of which is URL_A (URL_B is hidden). When they hit edit and update the URL_A field I want to run URL_A through the ConvertURL() function and use that output as the data for URL_B when the update is performed.
I appreciate the help in advance!
Use the RowUpdating event of the grid view and change the new value for URL_B. In that event you will have access to the old values of the row as well as the new values. You can change the new values before they are sent to the data source for the actual update.
Add the event handler to the grid view in the ASPX:
<asp:GridView ID="GridView1" runat="server"
...
OnRowUpdating="GridView1_RowUpdating">
And in the code file handle the event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Find the current value for URL_A
string urlA = (string)e.NewValues["URL_A"];
// Get the corresponding value for URL_B from the method
string urlB = ConvertURL(urlA);
// Set the new value for the URL_B column before it's sent to the SqlDataSource
e.NewValues["URL_B"] = urlB;
}

Resources