I have several forms, each with their own DDL, that I'm using inside a page. I have them in different forms because I need different data sources for each DDL. When I press the Submit button, it gives me an error that it can't find the control "ddlCategory". I assume it is because it is in a different formview control. Here is the markup:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<br />
<asp:FormView ID="FormView2" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource2" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView3" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource3" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
Here is the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView2.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView3.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [AttyID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}
Again, it tells me that it can't find the control ddlCategory. I thought about putting them all in one formview, but how would I make different datasources for each of the dropdown lists? I also tried not having any formview controls at all and just using "this.[ID of the ddl]" but that gave me a null reference value. Any ideas on how to make this work?
Here's how I did it. I ended up putting all the DDLs inside one formview (I didn't realize I could specify different datasources for DDLs within a formview that has its own datasource) and putting the datasource that uses the property to use the value from the previous DDL. Works like a charm now.
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
<br />
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
<br />
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="AddRec" />
And the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView1.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView1.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [Atty_ID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}
Related
I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
I have several forms, each with their own DDL, that I'm using inside a page. I have them in different forms because I need different data sources for each DDL. When I press the Submit button, it gives me an error that it can't find the control "ddlCategory". I assume it is because it is in a different form. Here is the markup:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<br />
<asp:FormView ID="FormView2" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource2" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView3" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource3" DefaultMode="Insert" >
<InsertItemTemplate>
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
Also, if there is a way to do it inside one formview control, I'd like to know that too.
Did it this way:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT * FROM [ORGANIZATIONS]"/>
<asp:AccessDataSource ID="AccessDataSource3" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ATTY_ID, NAME & ' ' & INITIAL & ' ' & LASTNAME AS Expr1 FROM ATTORNEYS ORDER BY NAME & INITIAL & ' ' & LASTNAME">
</asp:AccessDataSource>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID"
DataSourceID="AccessDataSource1" DefaultMode="Insert" >
<InsertItemTemplate>
Select a Category:<br />
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="ORG_NAME"
DataValueField="ID">
</asp:DropDownList>
<br />
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT ID,SectionName FROM ORG_SECTIONS WHERE OrgID=#OrgID ">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory"
PropertyName="SelectedValue"
Name="ID" Type="String"
DefaultValue="" />
</SelectParameters>
</asp:AccessDataSource>
Select an Organization:<br />
<asp:DropDownList ID="ddlOrg" runat="server"
DataSourceID="AccessDataSource2" DataTextField="SectionName"
DataValueField="ID">
</asp:DropDownList>
<br />
Select an Attorney:<br />
<asp:DropDownList ID="ddlAtty" runat="server"
DataSourceID="AccessDataSource3" DataTextField="Expr1" DataValueField="ATTY_ID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="AddRec" />
And the code behind:
protected void AddRec(object sender, EventArgs e)
{
DropDownList ddlCategory = (DropDownList)FormView1.FindControl("ddlCategory");
DropDownList ddlOrg = (DropDownList)FormView1.FindControl("ddlOrg");
DropDownList ddlAtty = (DropDownList)FormView1.FindControl("ddlAtty");
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webvideos.mdb;";
string cmdstr = "INSERT INTO [Org_Sec_Atty] ([OrgID], [SecID], [Atty_ID]) VALUES (?, ?, ?)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
con.Open();
com.Parameters.AddWithValue("#OrgID", ddlCategory.SelectedValue);
com.Parameters.AddWithValue("#SecID", ddlOrg.SelectedValue);
com.Parameters.AddWithValue("#AttyID", ddlAtty.SelectedValue);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("ManageProfAffs.aspx");
}
And done.
i have 3 web pages. admin,student and teacher. i have used grid view on each of this page.my table is having a column as 'status'.which is having default value 0. I want to update this value to 1 when i click on delete in gridview. I have written following code. But it is not working.pls help.
this is .aspx file code-
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TEMPRUJUConnectionString %>"
SelectCommand="SELECT * FROM [login] WHERE ([username] = #username)"
DeleteCommand="UPDATE [login] SET [status]= 1 WHERE [id]=#id"
InsertCommand="INSERT INTO [login] ([name], [midname], [surname], [username], [password], [contact], [dob], [email], [address], [occupation], [ltype]) VALUES (#name, #midname, #surname, #username, #password, #contact, #dob, #email, #address, #occupation, #ltype)"
UpdateCommand="UPDATE [login] SET [name] = #name, [midname] = #midname, [surname] = #surname, [username] = #username, [password] = #password, [contact] = #contact, [dob] = #dob, [email] = #email, [address] = #address, [occupation] = #occupation, [ltype] = #ltype WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
and this is my code behind-
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SNEHAL-PC\\SNEHAL1;Initial Catalog=TEMPRUJU;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
con.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
cmd=new SqlCommand("Update login set status=1 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",con);
dr = cmd.ExecuteReader();
}
when you have SqlDataSource you have no need to implement GridView1_RowDeleting,
first you must tell to SqlDataSource that where it can find the value of delete command parametr(s), in your case #id... for this, you need to set DataKeyNames property on your GridView by database unique key field name, in your case id...
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1">
then, to set delete command parameter on SqlDataSource you must tell that, find value of #id from SelectedDataKey property, from GridView like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TEMPRUJUConnectionString %>"
DeleteCommand="UPDATE [login] SET [status]= 1 WHERE [id]=#id">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
and finaly, you need to, send a Delete command with your gridView, for this, you can enable deleteing on gridview control to add this <asp:CommandField ShowDeleteButton="True" /> into your grid view markup, or you can convert this field into a TemplateFiled and create your own template with any control _which have_ CommandName property, and set that property (CommandName) to keyword Delete like this :
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete me"></asp:LinkButton>
this is a sample with Template Field:
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserID" SortExpression="UserID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("UserID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age" SortExpression="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
DeleteCommand="UPDATE Profiles SET Age = Age+1 where id = #id" SelectCommand="SELECT * FROM Profiles">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
and this is a sample with just enable deleteing :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
DeleteCommand="UPDATE Profiles SET Age = Age+1 where id = #id" SelectCommand="SELECT * FROM Profiles">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedDataKey" />
</DeleteParameters>
</asp:SqlDataSource>
i increment age in my sample by every delete command
After editing some fields, clicking on Update doesn't show the new values. I have tried two ways to retrieve the new values as you can see from this shortened version of the ItemUpdating event (and both return the old ones) :-
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim txtPassword As TextBox = CType(DetailsView1.Rows(1).Cells(1).FindControl("txtPassword"), TextBox)
Struct_Student.password = txtPassword.Text
Dim PasswordValue As String = e.NewValues("password").ToString()
Struct_Student.password = PasswordValue
End Sub
Here is a shortened version of the aspx :-
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<div id="Controls">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DetailsView ID="DetailsView1"
runat="server"
AutoGenerateRows="False"
DataKeyNames="student_id" DataSourceID="SqlDataSource1"
ForeColor="Blue"
BackColor="#FFF7E6"
AutoPostBack="True"
AutoGenerateEditButton = True
AutoGenerateInsertButton = True
OnModeChanging="StudentDetailView_ModeChanging"
Height=163px
Width=327px
style="left: 400px; top: 1px; position: absolute;">
<Fields>
<asp:TemplateField
HeaderText="Password">
<EditItemTemplate>
<asp:TextBox
id="txtPassword"
Text = '<%# Bind ("password") %>'
runat = "server" />
<asp:RequiredFieldValidator
ID = "reqPassword"
ControlToValidate = "txtPassword"
Text = "(required)"
Display = "Dynamic"
runat = "server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label
id="PasswordLabel"
runat="server"
Text = '<%# Eval("password") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField
datafield="emailaddress"
headertext="Email address"
SortExpression="emailaddress"
/>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:FCLManager %>" ProviderName="MySql.Data.MySqlClient"
SelectCommand="Select * from tblstudentinfo WHERE centre_id = #CentreID and fullname = #FullName"
<SelectParameters>
<asp:Parameter Name="CentreID" Type="Int16" DefaultValue="0" />
<asp:Parameter Name="FullName" Type="String" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div> <%--Controls div--%>
</asp:Content>
This code lets you search for a table name in an Oracle database. then when you select a row it puts all the columns for the selected table in the second gridview.
The Code below works if I only use one DatakeyName - "Table_Name" but it will display identical table names with different owners if they exist which is not what I want. I want to pull the details based on two fields - Table_Name and Owner.
I can't figure out how to get the details part to work with two datakeyNames.
<asp:Label ID="lblTitleSrchOracleTab" runat="server" Text="Search For A Table In Oracle"></asp:Label>
<br /><br />
<asp:Label ID="lblOracleTableName" runat="server" Text="Oracle Table Name"></asp:Label>
<asp:TextBox ID="txtOracleTableName" runat="server"></asp:TextBox>
<asp:Button ID="btnOracleTableName" runat="server" Text="Search"
/>
<br /><br />
<asp:GridView ID="gvOracleTableName" runat="server" CssClass="mGrid"
AutoGenerateSelectButton="True" AutoGenerateColumns="False"
DataSourceID="sdsOracleTableName" DataKeyNames="Owner,Table_Name" >
<Columns>
<asp:BoundField DataField="OWNER" HeaderText="OWNER" SortExpression="OWNER" />
<asp:BoundField DataField="TABLE_NAME" HeaderText="TABLE_NAME"
SortExpression="TABLE_NAME" />
<asp:BoundField DataField="NUM_ROWS" HeaderText="NUM_ROWS"
SortExpression="NUM_ROWS" />
<asp:BoundField DataField="TABLESPACE_NAME" HeaderText="TABLESPACE_NAME"
SortExpression="TABLESPACE_NAME" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsOracleTableName" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Owner, Table_name, Num_Rows, Tablespace_name
FROM all_tables
WHERE trim(upper(table_name)) LIKE trim(upper('%' || :TableName || '%'))">
<SelectParameters>
<asp:ControlParameter ControlID="txtOracleTableName" Name="TableName"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:GridView ID="gvSelectedTableColumns" runat="server" CssClass="mGrid"
AutoGenerateColumns="False" DataSourceID="sdsgvSelectedTableColumns">
<Columns>
<asp:BoundField DataField="OWNER" HeaderText="OWNER" SortExpression="OWNER" />
<asp:BoundField DataField="TABLE_NAME" HeaderText="TABLE_NAME"
SortExpression="TABLE_NAME" />
<asp:BoundField DataField="COLUMN_NAME" HeaderText="COLUMN_NAME"
SortExpression="COLUMN_NAME" />
<asp:BoundField DataField="DATA_TYPE" HeaderText="DATA_TYPE"
SortExpression="DATA_TYPE" />
<asp:BoundField DataField="DATA_LENGTH" HeaderText="DATA_LENGTH"
SortExpression="DATA_LENGTH" />
<asp:BoundField DataField="NULLABLE" HeaderText="NULLABLE"
SortExpression="NULLABLE" />
</Columns>
<SelectedRowStyle BorderColor="Red" Font-Bold="True" />
</asp:GridView>
<asp:SqlDataSource ID="sdsgvSelectedTableColumns" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Owner, table_name, column_name, Data_Type, Data_Length, Nullable
FROM all_tab_columns
WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND
trim(upper(table_name)) =trim(upper(:SelectedTableName))
">
<SelectParameters>
<asp:ControlParameter ControlID="gvOracleTableName" Name="SelectedOwner"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="gvOracleTableName" DefaultValue=""
Name="SelectedTableName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Can't you extend the where clause?
So
from all_tables where ... and owner = 'MYOWNER'
or
select from user_tables instead of all_tables.
that's what this is
WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND
trim(upper(table_name)) =trim(upper(:SelectedTableName))
my problem is that when you use the sqlDataSource wizard to connect the paramters to the gridview control, it doesn't let you select which datakey of the gridview to bind it to.
I finally figured this one out. You can't Mighty Mouse it - it takes actual code! Specifically the gridview_SelectedIndexChanged event.
Here's how I did it.
protected void gvOracleTableName_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedOwner;
SelectedOwner = gvOracleTableName.SelectedRow.Cells[1].Text ;
string SelectedTableName;
SelectedTableName = gvOracleTableName.SelectedRow.Cells[2].Text;
lblTest.Text = SelectedOwner + " " + SelectedTableName;
string strConn, strSQL;
strConn = #"";
strSQL = #"SELECT Owner, table_name, column_name, Data_Type, Data_Length, Nullable FROM all_tab_columns WHERE trim(upper(Owner)) =trim(upper(:SelectedOwner)) AND trim(upper(table_name)) =trim(upper(:SelectedTableName))";
using (OracleConnection cn = new OracleConnection(strConn))
{
OracleCommand cmd = new OracleCommand(strSQL, cn);
cmd.Parameters.AddWithValue(":SelectedOwner", SelectedOwner);
cmd.Parameters.AddWithValue(":SelectedTableName", SelectedTableName);
cn.Open();
OracleDataReader rdr = cmd.ExecuteReader();
gvSelectedTableColumns.DataSource = rdr;
gvSelectedTableColumns.DataBind();
cn.Close();
}
}