Filling GridView Based on DropDownList selected Value (vb.net) - asp.net

I have a drop down list that has inventory material groups, and I would like there to be a way that when I select a different group, the grid view loads the materials for that group. I'm a little confused on how to accomplish this...
ASPX
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Design_Construction_V2ConnectionString %>" ProviderName="<%$ ConnectionStrings:Design_Construction_V2ConnectionString.ProviderName %>" SelectCommand="SELECT [UnitPriceInfo], [groupNumber], [itemDescription], [CrossTieType], [AFEFunctionCode], [AFECode], [unitType], [unitPrice] FROM [itemsList_]" FilterExpression="groupNumber = {0}">
<FilterParameters>
<asp:ControlParameter Name="groupNumber" ControlID="ddlMaterials" PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</FilterParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" ID="ddlMaterials"></asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="UnitPriceInfo" HeaderText="UnitPriceInfo" SortExpression="UnitPriceInfo" />
<asp:BoundField DataField="groupNumber" HeaderText="groupNumber" SortExpression="groupNumber" />
<asp:BoundField DataField="itemDescription" HeaderText="itemDescription" SortExpression="itemDescription" />
<asp:BoundField DataField="CrossTieType" HeaderText="CrossTieType" SortExpression="CrossTieType" />
<asp:BoundField DataField="AFEFunctionCode" HeaderText="AFEFunctionCode" SortExpression="AFEFunctionCode" />
<asp:BoundField DataField="AFECode" HeaderText="AFECode" SortExpression="AFECode" />
<asp:BoundField DataField="unitType" HeaderText="unitType" SortExpression="unitType" />
<asp:BoundField DataField="unitPrice" HeaderText="unitPrice" SortExpression="unitPrice" />
</Columns>
</asp:GridView>
CODEBEHIND
Private Sub ddlMaterials_Load(sender As Object, e As EventArgs) Handles ddlMaterials.Load
If Not IsPostBack Then
Dim db As New DesignConstructionDataContext
Dim materials = (From m In db.Estimate_Groups
Where (m.BigGroup = "Materials")
Select m.groupName)
ddlMaterials.DataSource = materials
ddlMaterials.DataBind()
End If
End Sub
Private Sub ddlMaterials_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlMaterials.SelectedIndexChanged
End Sub

Change FilterParameters to SelectParameters. In where clause use this - where groupNumber = #groupNumber. So changed aspx will be like following.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Design_Construction_V2ConnectionString %>" ProviderName="<%$ ConnectionStrings:Design_Construction_V2ConnectionString.ProviderName %>" SelectCommand="SELECT [UnitPriceInfo], [groupNumber], [itemDescription], [CrossTieType], [AFEFunctionCode], [AFECode], [unitType], [unitPrice] FROM [itemsList_] where groupNumber = #groupNumber">
<SelectParameters>
<asp:ControlParameter Name="groupNumber" ControlID="ddlMaterials" PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>

Related

Reading data from a grid view into text boxes vb

I'm trying to read data retrieved into a gridview out to textboxes. My idea is to hide the gridview as im using it to retrieve data from a table that i need displayed in textboxes but i don't want the gridview to be shown.
So far i have tried this code:
Protected Sub btnFindRepair_Click(sender As Object, e As EventArgs) Handles btnFindRepair.Click
Dim row1 As GridViewRow = GridView1.SelectedRow
txtFname.Text = row1.Cells(3).Text
txtLname.Text = row1.Cells(4).Text
txtContactNum.Text = row1.Cells(5).Text
txtAltContactNum.Text = row1.Cells(6).Text
txtAddress.Text = row1.Cells(7).Text
End Sub
However this gives me the following error
An exception of type 'System.NullReferenceException' occurred in App_Web_lfjfpvke.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object
I have checked all of objects and instances and can't seem to find where the null value would come from
This is my gridview code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="Repair_ID">
<Columns>
<asp:BoundField DataField="Repair_ID" HeaderText="Repair_ID" SortExpression="Repair_ID" InsertVisible="False" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Booking_Number" HeaderText="Booking_Number" SortExpression="Booking_Number" Visible="False" />
<asp:BoundField DataField="Tracking_Number" HeaderText="Tracking_Number" SortExpression="Tracking_Number" Visible="False" />
<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="Contact_Number" HeaderText="Contact_Number" SortExpression="Contact_Number" />
<asp:BoundField DataField="Alternative_Contact_Number" HeaderText="Alternative_Contact_Number" SortExpression="Alternative_Contact_Number" />
<asp:BoundField DataField="Customer_Address" HeaderText="Customer_Address" SortExpression="Customer_Address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>" SelectCommand="SELECT * FROM [Customer] WHERE ([Tracking_Number] = #Tracking_Number)">
<SelectParameters>
<asp:ControlParameter ControlID="txtTrackingNumber" Name="Tracking_Number" PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

How To Insert USERID (Type: Raw, Size: 16) from Ora_aspnet_users to Another Table

Sorry I cannot upload the screenshot due to the limited number of my reputation since I am a newbie here.
ora_aspnet_user table with column name userid with datatype of Raw(16)
link to
Instruct Table with userid with datatype of Raw (16) - Other columns is ID (Auto Trigger No), Command (Varchar2 = 256)
MY INLINE ASPX CODE:
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="COMMAND" HeaderText="COMMAND" SortExpression="COMMAND" />
<asp:TemplateField HeaderText="USERID">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="USERNAME" DataValueField="USERID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
InsertCommand="INSERT INTO INSTRUCT (COMMAND, USERID) VALUES (:COMMAND, :USERID)">
<InsertParameters>
<asp:Parameter Name="COMMAND" Type="String" />
<asp:Parameter Name="USERID" DbType="Binary" Size="16" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT USERID, USERNAME FROM ORA_ASPNET_USERS">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
The above code will only insert the command textbox and but will not insert the USERID from the USERID dropdown.
I would really appreciate your help.
Thank you very much.
Replace your datasource <InsertParameter> with this one, it might help you, because I don't have your source code I cannot test it locally, the problem could occur somewhere elese though:
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="USERID" Type="Byte"></asp:ControlParameter>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedItem.Text" Name="COMMAND" Type="String"></asp:ControlParameter>
</InsertParameters>

InsertCommand into Header Template

Trying to insert three textboxes into the header of a gridview and with a button execute an "INSERT INTO" into the database.
In my online server database: idt is int, datetime is varchar, and col1,col2,col3 are numeric.
<asp:SqlDataSource
id="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:connone %>"
SelectCommand="SELECT * FROM [test];"
InsertCommand="INSERT INTO [test] [datetime],[col1],[col2],[col3] VALUES #datetime,#col1,#col2,#col3;"
runat="server">
<InsertParameters>
<asp:Parameter Name="datetime" Type="String" />
<asp:Parameter Name="col1" Type="Double" />
<asp:Parameter Name="col2" Type="Double" />
<asp:Parameter Name="col3" Type="Double" />
</InsertParameters>
</asp:SqlDataSource>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataSource1.InsertParameters("#datetime").DefaultValue = DateTime.Now.ToString()
SqlDataSource1.InsertParameters("#col1").DefaultValue = CType(GridView1.HeaderRow.FindControl("TextBoxHeadercol1"), TextBox).Text
SqlDataSource1.InsertParameters("#col2").DefaultValue = CType(GridView1.HeaderRow.FindControl("TextBoxHeadercol2"), TextBox).Text
SqlDataSource1.InsertParameters("#col3").DefaultValue = CType(GridView1.HeaderRow.FindControl("TextBoxHeadercol3"), TextBox).Text
SqlDataSource1.Insert()
End Sub
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False"
DataKeyNames="idt">
<Columns>
<asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
<asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
<asp:TemplateField SortExpression="col1">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol1" text="col1" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col2">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol2" text="col2" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="col3">
<HeaderTemplate>
<asp:TextBox ID="TextBoxHeadercol3" text="col3" runat="server" MaxLength="40" />
</HeaderTemplate>
</asp:TemplateField>
</asp:GridView>
I suggest to remove "#" in InsertParameters as follows:
SqlDataSource1.InsertParameters("col1").DefaultValue =
((TextBox)GridView1.HeaderRow.FindControl("TextBoxHeadercol1")).Text;
this should work.

DetailsView not working with selectable GridView

My DetailsView is always blank. I've tested both Data Sources in QueryBuilder and they return values. I suspect something is wrong with the parameter value assignments in PoweredEquipDetails where they are set to the SelectedValue in GridView1, but I don't know why it wouldn't work.
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="PoweredEquipHeader" DataKeyNames="Model,Serial">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Model" HeaderText="Model" ReadOnly="True"
SortExpression="Model" />
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
</Columns>
<SelectedRowStyle BackColor="#FF6600" />
</asp:GridView>
<asp:SqlDataSource ID="PoweredEquipHeader" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="Select t1.Model, t1.Serial From Powered_Equip t1 Where TechID = #TechID And Store = #Store">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="TECH01" Name="TechID"
QueryStringField="Tech" />
<asp:QueryStringParameter DefaultValue="0000" Name="Store"
QueryStringField="Store" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="PoweredEquipDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="SELECT t1.Serial, t1.Model, t1.Status, t1.[Trailer VIN], t2.Tag, t2.State, t2.ExpMonth, t2.ExpYear FROM Powered_Equip AS t1 INNER JOIN Trailers AS t2 ON t1.[Trailer VIN] = t2.VIN WHERE (t1.Serial = #Serial) AND (t1.Model = #Model)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="PoweredEquipDetail" DataKeyNames="Serial,Model">
<Fields>
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
<asp:BoundField DataField="Model" HeaderText="Model"
ReadOnly="True" SortExpression="Model" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="Trailer VIN" HeaderText="Trailer VIN" ReadOnly="True"
SortExpression="Trailer VIN" />
<asp:BoundField DataField="Tag" HeaderText="Tag" ReadOnly="True"
SortExpression="Tag" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True"
SortExpression="State" />
<asp:BoundField DataField="ExpMonth" HeaderText="ExpMonth" ReadOnly="True"
SortExpression="ExpMonth" />
<asp:BoundField DataField="ExpYear" HeaderText="ExpYear" ReadOnly="True"
SortExpression="ExpYear" />
</Fields>
</asp:DetailsView>
</form>
EDIT: So I believe what is happening is the GridView's SelectedValue in the DetailsView parameter assignments is always Model, instead of both Model and Serial as specified in my DataValueKeys. In the event that an equipment item has a model and serial that are the same value (i.e. Model 118 Serial 118), the DetailsView populates. But in any case where the Model and Serial are different (which will be almost every other case), the DetailsView is blank. So how do I get two different SelectedValues to assign value to two different ControlParameters?
Changed
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
to
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedDataKey.Values[Serial]" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
int n = int.Parse(GridView1.SelectedValue.ToString());
TextBox4.Text = n.ToString();
con = new SqlConnection(#"Data Source=LAB-III-136;Initial Catalog=student;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("select * from student1 where id='" + TextBox4.Text + " '", con);
ds = new DataSet();
da.Fill(ds, "temp");
DetailsView2.DataSource = ds;
DetailsView2.DataBind();

Multiple DataKeyNames in Master Detail Gridview

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();
}
}

Resources