views:

1700

answers:

2

I want to implement the following scenario...

Table related to this question is as follows:

ProfileID int (identity) FirstName varchar(100) LastName varchar(100)

step 1. User visits profiles.aspx page. Here in a grid view, he can see other people profiles. (I know how to do this.)

Step 2. User visits MyProfile.aspx page. Since his profile does not exist, details view, is blank with insert button enabled. User clicks the insert button and he can add his own profile only. (I dont know how to do this).

Step 3. Once users profile is added, it shows up on profiles.aspx page. He wants to update his own profile. He navigates to MyProfile.aspx page with ProfileID lets say 33. DetailsView is able to pull up his profile based on profileid and update button is enabled. (I dont know how how to do this.)

Could some one please help me with step 2 and 3. I dont know how to setup sqldatasource and detailsview to accomplish this.

Thank you in advance.

A: 

Hi there,

Alright , I'll give you a simple solution which you can alter to fit to your needs.

For the Step 2: You create a page which called create.aspx for example; in that page you put all the text boxes that you need and a button for registration. On the clicked event of that button write something as below

//using System.Data;
//using System.Data.Sql; you have to import these

//Put connection string
SqlConnection sqlcon = new SqlConnection("your connection string here which will be able to find in MSDN");
sql.Open();
SqlCommand sqlcmd = new SqlCommand("INSERT INTO Statement which easily find in MSDN too",sqlcon);
sqlcmd.ExecuteNoneQuery();
sql.Close();

Which this code you can insert into database whatever you want. In the INSERT INTO string you concatenate your inputs but be very careful that may cause SqlInjection bug which is very dangerous.(Please read articles about it).

For Step 3: everything is all the same except instead of putting "INSERT ...." statement you have to use "UPDATE ..." statement which can find on MSDN.

Regards, Pooria.

Pooria
A: 

see following example........ source file .aspx i am using gridview and detailsview for insert,update,edit a truckmaster table. do the following steps: 1.take a gridview.assign datakeynames as your unique column from table.you can write boundfields for your each field.Infact that is better way. 2. take datasource control.Bind it to your table. 3.bind this datasource to Gridview.you will understand this steps better once you go through the following code. 4.Take a detailsview.Assign datakeynames as your unique column from table. 5.Take another datasource.Bind it to same table.declare insertcommand,selectcommand. declare select parameter,updateparameter as in below code. Hope it will help you.

<asp:GridView id                  ="gridTruckMaster" 
          runat               ="server" 
          CssClass            ="GridView" 
          DataKeyNames        ="TruckType" 
          DataSourceID        ="SqlDataSource1" 
          AutoGenerateColumns ="False" 
          ShowFooter          ="True" 
          AllowPaging         ="True">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True"    ForeColor="White"></FooterStyle>
<RowStyle    VerticalAlign="Top" BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<Columns>
<asp:BoundField   DataField="TruckType"     HeaderText="TruckType"     SortExpression="TruckType"></asp:BoundField>
<asp:BoundField   DataField="TruckCapacity" HeaderText="TruckCapacity" SortExpression="TruckCapacity"></asp:BoundField>
<asp:CommandField ShowSelectButton="True"></asp:CommandField>
</Columns>
<PagerStyle       BackColor="#284775" HorizontalAlign="Center"   ForeColor="White"></PagerStyle>
<SelectedRowStyle BackColor="#669999" Font-Bold="True"           ForeColor="White"></SelectedRowStyle>
<HeaderStyle      BackColor="#5D7B9D" Font-Bold="True"           ForeColor="White"></HeaderStyle>
</asp:GridView> 

<asp:SqlDataSource ID               ="SqlDataSource1" 
                   runat            ="server" 
                   ConnectionString ="<%$ ConnectionStrings:GodrejReporterConnectionString %>"
                   SelectCommand    ="SELECT [TruckType], [TruckCapacity] FROM [tbl_TruckMaster]"></asp:SqlDataSource>


<asp:DetailsView  id               ="dtlviewTruckMaster"   
              runat            ="server"         
              CssClass         ="GridView" 
              Width            ="125px" 
              DataKeyNames     ="TruckType" 
              Height           ="50px" 
              AutoGenerateRows ="False"
              HeaderText       ="Truck Details"
        ><Fields>
<asp:BoundField   DataField="TruckType"     HeaderText="TruckType" ReadOnly="True"></asp:BoundField>
<asp:BoundField   DataField="TruckCapacity" HeaderText="TruckCapacity"></asp:BoundField>
<asp:CommandField ShowInsertButton="True"></asp:CommandField>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
</Fields>
<FooterStyle     BackColor="#5D7B9D"      Font-Bold="True"    ForeColor="White"></FooterStyle>
<RowStyle        VerticalAlign="Top"      BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
<EditRowStyle    Width="250px"            Height="10px"/>
<InsertRowStyle  Width="250px"            Height="10px" />
<PagerStyle      HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>
<HeaderStyle     BackColor="#5D7B9D"      Font-Bold="True"    ForeColor="White"></HeaderStyle>
<CommandRowStyle BackColor="#5D7B9D" />
</asp:DetailsView> 

<asp:SqlDataSource ID               ="SqlDataSource2" 
                   runat            ="server" 
                   ConnectionString ="<%$ ConnectionStrings:GodrejReporterConnectionString %>"
                   SelectCommand    ="SELECT [TruckType], [TruckCapacity] FROM [tbl_TruckMaster] where TruckType=@pTruckType"
                   insertcommand    ="insert into tbl_TruckMaster(TruckType,TruckCapacity) values(@TruckType,@TruckCapacity)"
                   updatecommand    ="update tbl_TruckMaster set TruckCapacity=@TruckCapacity where TruckType=@pType"
    >
    <SelectParameters>
        <asp:ControlParameter ControlID="gridTruckMaster" Name="pTruckType" PropertyName="SelectedValue" />
    </SelectParameters>
    <UpdateParameters>
        <asp:ControlParameter ControlID="gridTruckMaster" Name="pType"      PropertyName="SelectedValue" />
    </UpdateParameters>
</asp:SqlDataSource>

If you getting problem you can mail me at [email protected]