mardi 9 mai 2017

How to show the name and not the value in Gridview Asp c#

I have a website with 3 pages, First ahead is a loginpage with username and password (no problems works fine)

The second page I have a page made out of

1 checkbox not giving its value which should be yes or no (shown inside Gridview) 2 comboboxes with items. - the first combobox has 10 items which show the name and not the value inside of the combobox(inside of gridview it shows values but should also show the name) the second combobox has 90 items which sort unfortunatly after an entire page reload from the first one.(inside of gridview it shows values but should also show the name)

So both comboxes show their number values not their names.. how can you show their names

On the third page I have a gridview page which should show all the data from the sql database with their names and not values. Even the username name not their values.(and the weird part is even though I login with the same username it always shows another value in gridview which I think should be always the same since its the same user. But it should also show the Name and not the value

I have made the databases with SQL server linked it with sqldatasource.

Where is it going wrong?

  • In Gridview it should show not the value(number) of the user who is logged in but the name of that person.

  • In gridview it should show the checkbox with Yes(checked) or No(unchecked)

  • Which also goes for the two comboboxes need the name which is chosen not the value number.

Iv'e tried everything and I cant seem to make sence Havent even slept today... taf

LoginTable

CREATE TABLE [dbo].[UserList] (

 [ID]        INT        IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,

 [UsersName] NCHAR (50) NOT NULL,

 [Pass]      NCHAR (50) NOT NULL

 );

CREATE TABLE [dbo].[Form] (

[CVO STEP]           NVARCHAR (50) NULL,

[ID]                 INT           IDENTITY (1, 1) NOT NULL,

[Afdeling]           NVARCHAR (50) NULL,

[Opleiding]          NVARCHAR (50) NULL,

[Doel]               VARCHAR (200) NULL,

[Doelgroep]          VARCHAR (200) NULL,

[Boodschap]          VARCHAR (200) NULL,

[Concurrentie]       VARCHAR (200) NULL,

[Communicatiemiddel] VARCHAR (200) NULL,

[Periode Van]        DATE          NULL,

[Periode Tot]        DATE          NULL );

CREATE TABLE [dbo].[tblAfdelingen] (

[Id]   INT           IDENTITY (1, 1) NOT NULL,

[Name] NVARCHAR (50) NULL,

PRIMARY KEY CLUSTERED ([Id] ASC) );

CREATE TABLE [dbo].[tblOpleidingen] (

[Id]         INT           IDENTITY (1, 1) NOT NULL,

[Name]       NVARCHAR (50) NULL,

[AfdelingID] INT           NULL,

PRIMARY KEY CLUSTERED ([Id] ASC),

FOREIGN KEY ([AfdelingID]) REFERENCES [dbo].[tblAfdelingen] ([Id]) );

Login page first page

    <div class="imgcontainer" style="background-color:#f1f1f1">

        &nbsp;<img src="Images/logo.png"  alt="Avatar" class="avatar" />
    <table style="background-color:#f1f1f1" class="auto-style3">
    <tr>
        <td> Gebruikersnaam</td>
        <td class="auto-style2"><asp:TextBox ID="Textbox1" runat="server" name="uname"></asp:TextBox></td>
    </tr>
    <tr>
        <td> Paswoord</td>
        <td class="auto-style2"><asp:TextBox ID="Textbox2" runat="server" name="psw" TextMode="Password"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan="2" align="right" class="auto-style1">
           <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" Width="122px" CssClass="auto-style4" />
           <asp:Label  runat="server" ID="txt_error" Visible="false" Text="" Width="230px"> </asp:Label>
        </td>
    </tr>
    </table>
    </div>        
</form>

Code behing login page the first page

public partial class Login : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();

    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();


    protected void Page_Load(object sender, EventArgs e)
  {
        if (IsPostBack)
            this.txt_error.Text = "Gebruikersnaam/Paswoord Incorrect";
        this.txt_error.Visible = true;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        UserList userLogIn = null;
        try
        {
            string user = Textbox1.Text;
            string pass = this.Textbox2.Text;

            if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(pass))
            {
                using (var db = new LoginEntities1())
                {
                    userLogIn = db.UserLists.FirstOrDefault(x => x.UsersName == user && x.Pass == pass);
                }
                if (userLogIn != null)
                {
                    Session["Gebruikersnaam"] = true;
                    Response.Redirect("Index.aspx", false);
                }
            }
        }
        catch (Exception ex)
        {
             throw ex;
        }

    }
}

}

The submit or second page

        <asp:CheckBox ID="CheckBox1" visible="true" runat="server" Value="CVO Step" Text="CVO Step" Checked="true" onclick="ShowValue(CVO);"/>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label10" runat="server" Text="Afdeling"></asp:Label>
        <ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoPostBack="True" placeholder="-Selecteer Afdeling-" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" style="display: inline;"></ajaxToolkit:ComboBox>
         <asp:Label ID="Label11" runat="server" Text="Opleiding"></asp:Label>
        <ajaxToolkit:ComboBox ID="ComboBox2" runat="server" DataSourceID="SqlDataSource2" placeholder="-Selecteer Opleiding-" DataTextField="Name" DataValueField="AfdelingID" MaxLength="0" style="display: inline;" Width="200px"></ajaxToolkit:ComboBox>
        <br>
        <br>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LijstConnectionString2 %>" SelectCommand="SELECT * FROM [tblAfdelingen]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LijstConnectionString3 %>" SelectCommand="SELECT * FROM [tblOpleidingen] WHERE ([AfdelingID] = @AfdelingID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="ComboBox1" Name="AfdelingID" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
         <br>
         <asp:Label ID="Label2" runat="server" Text="Doel"></asp:Label><br>
          <asp:TextBox ID="txt_Textbox2" runat="server" height="20px" Width="300px" placeholder="Wat is het doel?" MaxLength="200" ></asp:TextBox><br>  

         <asp:Label ID="Label3" runat="server" Text="Doelgroep"></asp:Label><br> 
          <asp:TextBox ID="txt_Textbox3" runat="server" height="20px" Width="300px" placeholder="Wie is de doelgroep?" MaxLength="200"></asp:TextBox><br> 

          <asp:Label ID="Label4" runat="server" Text="Boodschap"></asp:Label><br> 
           <asp:TextBox ID="txt_Textbox4" runat="server" height="20px" Width="300px" placeholder="Wat is de boodschap?" MaxLength="200"></asp:TextBox><br> 

         <asp:Label ID="Label5" runat="server" Text="Concurrentie"></asp:Label><br> 
          <asp:TextBox ID="txt_Textbox6" runat="server" height="20px" Width="300px" placeholder="Wie is de concurrentie?" MaxLength="200"></asp:TextBox><br> 

          <asp:Label ID="Label6" runat="server" Text="Communicatiemiddel">Communicatiemiddel/Budgetinfo</asp:Label><br> 
          <asp:TextBox ID="txt_TextBox5" runat="server" height="20px" Width="300px" placeholder="Communicatiemiddel en Budget?" MaxLength="200"></asp:TextBox><br>
   </div>

  <div class='container'>
        <div class="form-group" >
    <div class='input-group date' id='datetimepicker1'>
        <asp:Label ID="Label13" runat="server" Text="Periode Van"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label12" runat="server" Text="Tot:"></asp:Label>
        <br>

    <asp:TextBox ID="TextBox1" runat="server" Width="146px"/>
    <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" Format="MM/dd/yyyy" PopupButtonID="TextBox1" />

    <asp:TextBox ID="TextBox2" runat="server" Width="146px"/>
    <ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="TextBox2" Format="MM/dd/yyyy" PopupButtonID="TextBox2" />
    <br> 
    <br>
    </div>
  </div>
</div>
<asp:Button ID="Button2" runat="server" OnClientClick="return confirm('Aanvraag wordt verstuurd, Bent u zeker?')" Text="Verzend" OnClick="Button2_Click" />

 <br><br>
 <br><br>
 <br><br>
 <br><br>


Code Behind submit page

{

public partial class HBO : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(@"Data Source=("");

    protected void Button2_Click(object sender, EventArgs e)
    {

        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = cmd.CommandText = "insert into Form values('" + CheckBox1.Text + "', '" + ComboBox1.Text + "', '" + ComboBox2.Text + "', '" + txt_Textbox2.Text + "', '" + txt_Textbox3.Text + "', '" + txt_Textbox4.Text + "', '" + txt_Textbox6.Text + "', '" + txt_TextBox5.Text + "', '" + TextBox1.Text + "', '" + TextBox2.Text + "');";
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("Overzicht.aspx");

    }


}

}

The Gridviewpage

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" ShowFooter="True" ShowHeaderWhenEmpty="True" Enabled="False" HorizontalAlign="Center" Width="1200px">
     <Columns>
        <asp:BoundField DataField="CVO STEP" HeaderText="CVO STEP" SortExpression="CVO STEP" />
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" />
        <asp:BoundField DataField="Afdeling" HeaderText="Afdeling" SortExpression="Afdeling" />
        <asp:BoundField DataField="Opleiding" HeaderText="Opleiding" SortExpression="Opleiding" />
        <asp:BoundField DataField="Doel" HeaderText="Doel" SortExpression="Doel" />
        <asp:BoundField DataField="Doelgroep" HeaderText="Doelgroep" SortExpression="Doelgroep" />
        <asp:BoundField DataField="Boodschap" HeaderText="Boodschap" SortExpression="Boodschap" />
        <asp:BoundField DataField="Concurrentie" HeaderText="Concurrentie" SortExpression="Concurrentie" />
        <asp:BoundField DataField="Communicatiemiddel" HeaderText="Communicatiemiddel/Budget" SortExpression="Communicatiemiddel" />
        <asp:BoundField DataField="Periode Van" HeaderText="Periode Van" SortExpression="Periode Van" DataFormatString="{0:MM/dd/yy}" />
         <asp:BoundField DataField="Periode Tot" HeaderText="Periode Tot" SortExpression="Periode Tot" DataFormatString="{0:MM/dd/yy}" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:InhoudConnectionString %>" SelectCommand="SELECT * FROM [Form]" ></asp:SqlDataSource>

Code behind

public partial class TKO : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (GridView1.DataSource == null)
        {
            GridView1.DataSource = new string[] { };
        }

    }

    //protected void GridView1_GridViewRow(object sender, EventArgs e)
   // {
   //     DataRowView rowView = (DataRowView)GridView1.Rows[0].DataItem;
   //     string name = rowView["2"].ToString();
   // }

    protected void GridviewBind()
    {
        using (var db = new InhoudEntities1())
        {
            var result = db.Forms.ToList();

            GridView1.DataSource = result;
            GridView1.DataBind();
        }





Aucun commentaire:

Enregistrer un commentaire