Showing posts with label sqldatasource. Show all posts
Showing posts with label sqldatasource. Show all posts

Tuesday, March 27, 2012

Datasource to Populate DDL - then choose item

I've got a page with a SQLDataSource control successfully populating a Dropdownlist...

However, now I find I need extended functionality - so I've got another page with links to this page, using querystrings - I need to hit the page, check the querystring, and if it's blank, just continue, but if the querystring is populated, then choose that item.

I've tried the page_load and the page_prerender, but so far, it's not working:
CC = Request.QueryString("center")
If CC <>""Then
' DropDownList1.Items.FindByText(CC).Selected = True
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(CC))
End if

I've double checked, and yes, every time CC is populated, that item is definitely in the dropdownlist - it just doesn't get changed to that item.

ideas?

EndIf

Here's an example for you:

ASPX

<asp:dropdownlist id="ddlPets" runat="server" ondatabound="ddlPets_DataBound"></asp:dropdownlist><br /><br /><asp:hyperlink id="hypDog" runat="server" navigateurl="1131233.aspx?pet=Dog" text="Pre-select Dog" /><br /><asp:hyperlink id="hypCat" runat="server" navigateurl="1131233.aspx?pet=Cat" text="Pre-select Cat" /><br /><asp:hyperlink id="hypGoldfish" runat="server" navigateurl="1131233.aspx?pet=Goldfish"text="Pre-select Goldfish" /><br />

CODE-BEHIND

protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){string[] pets = {"Dog","Cat","Goldfish" };ddlPets.DataSource = pets;ddlPets.DataBind();}}protected void ddlPets_DataBound(object sender, EventArgs e){if (this.Request["Pet"] ==null) {return; }ddlPets.SelectedIndex = ddlPets.Items.IndexOf(ddlPets.Items.FindByValue(this.Request["Pet"]));}
|||

using the databound even worked

sql

Wednesday, March 7, 2012

DataBinding: System.Data.DataRowView does not contain a property with the name RMID.

I m trying to UPDATE database using FormView and SqlDataSource and here is my code:

<

asp:SqlDataSourceID="sqlDS1"runat="server"ConnectionString="<%$ ConnectionStrings:myDB %>"UpdateCommand="UPDATE [Consultants] SET [firstName]=@.FIRSTNAME,[lastName]=@.LASTNAME,[skillCategoryID]=@.SKILLCATID,[resourceManagerID]=@.RMID,[AMgroupID]=@.AMGROUPID,[SkillSet]=@.SKILLSET,[statusID]=@.STATUSID,[location]=@.LOCATION,[comments]=@.COMMENTS,[profile]=@.PROFILE,[isAvailable]=@.ISAVAILABLE,[dateModified]=getdate(),[focusID]=1 WHERE ([id]=@.ID)">

<

UpdateParameters><asp:FormParameterFormField="txtFName"Name="FIRSTNAME"/><asp:FormParameterFormField="txtLName"Name="LASTNAME"/><asp:FormParameterFormField="ddlSkillCat"Name="SKILLCATID"/><asp:FormParameterFormField="ddlRM"Name="RMID"/><asp:FormParameterFormField="ddlAMGroup"Name="AMGROUPID"/><asp:FormParameterFormField="txtSkillset"Name="SKILLSET"/><asp:FormParameterFormField="ddlStatus"Name="STATUSID"Type="Int16"/><asp:FormParameterFormField="txtLocation"Name="LOCATION"/><asp:FormParameterFormField="txtComments"Name="COMMENTS"/><asp:FormParameterFormField="txtProfile"Name="PROFILE"/><asp:FormParameterFormField="cbAvailable"Name="ISAVAILABLE"/><asp:QueryStringParameterType="Int32"Name="ID"QueryStringField="id"/></UpdateParameters></asp:SqlDataSource>

//*******************************************************************

<

asp:FormViewDefaultMode="Edit"ID="FormView1"runat="server"DataSourceID="sqlDS1"DataKeyNames="id"><EditItemTemplate>

<

tr><tdclass="blacktextbold">

Resource Manager:

</td><tdclass="blacktext"><asp:DropDownListID="ddlRM"CssClass="blacktext"runat="server"DataSource="<%#GetRM()%>"DataTextField="RMName"DataValueField="userID"SelectedValue="<%#Bind('RMID')%>"AppendDataBoundItems="true"><asp:ListItemValue="-1">--Select RM--</asp:ListItem></asp:DropDownList><asp:CustomValidatorID="cvRM"runat="server"ValidationGroup="gpInsert"ControlToValidate="ddlRM"ClientValidationFunction="validateDropdown"OnServerValidate="servervalidateDropdown"Display="Dynamic"ErrorMessage="Required Field"CssClass="redtextsmallbold"/></td></tr>

//********************************************************

here is my GetRM() function:

protected

DataSet GetRM()

{

string strConnection =ConfigurationManager.ConnectionStrings["myDB"].ToString();SqlConnection objConnection =newSqlConnection(strConnection);String sqlSkillCats ="SELECT Roles.roleID, Roles.roleName, UsersInRoles.userID, UsersInRoles.roleID AS Expr1, Users.firstName + ' ' + Users.lastName AS RMName, Users.id AS Expr2" +" FROM Users INNER JOIN" +" UsersInRoles ON Users.id = UsersInRoles.userID CROSS JOIN" +" Roles" +" WHERE (Roles.roleName = 'accountmanager') AND (UsersInRoles.roleID = Roles.roleID) ORDER BY Users.firstName";SqlDataAdapter objAdapter =newSqlDataAdapter(sqlSkillCats, objConnection);

objConnection.Open();

//ddlDataSet = new DataSet();try

{

objAdapter.Fill(dsConsultantRM,

"ConsultantRM");

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

objConnection.Close();

return dsConsultantRM;

}

//******************************************************************

i get this error when i load the page:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'RMID'.

basically i m trying to UPDATE database using the value selected in the DropDownList, can anyone tell me whats wrong here...PLZ HELP!

I don't see a SelectCommand on your sqldatasource.|||

select command was in the function which returned the dataset. But that wasnt the issue, after changing all the CAPS parameter names to the exact name of the columns in my database i dint get any error!!!! i wonder why, for example i changed this:

<asp:parameter formfield="ddlRM" name="RMID"/>
TO
<asp:parameter formfield="ddlRM" name="resourceManagerID"/>

and then SelectedValue="<%#Bind('resourceManagerID')%>

Sunday, February 26, 2012

Databinding question

Hi,

I have a page created within VS 2005 which uses a detailsView with a SQLDataSource which has insert, edit and delete items allowed with it.

The problem is that if I delete a record I dont want to refresh the page as I want to set a label value to say item deleted. The problem then though is to select the item to delete I have a drop down which populates the details view on index change, but if I delete the item I cannot do a databind when its complete because it just binds to the existing dataset and does not do a fresh call on the database.

Is there a command I can run to refresh the dataset on click of the delete button?

Thanks

I think I would do databinding in a sub that does nothing else but the databinding. I normally do this in a sub called something like sub bindcontrols() or something like that. Then when I need to refresh the data I can just call that sub.|||

Thanks for your reply.

The problem is the databinding is handled by VS and I dont think I have a choice of where it runs.

|||try the following after deleting detailsView.databind() it should refresh the content Hope this helps