Gridview, Checkbox and SQL Parameters base on LoginName

Last post 11-07-2009 3:40 PM by Allison. 5 replies.

Sort Posts:

  • Gridview, Checkbox and SQL Parameters base on LoginName

    11-03-2009, 5:38 AM
    • Member
      3 point Member
    • Allison
    • Member since 09-11-2009, 12:29 AM
    • Posts 9

     Hello,

    I am creating a ASP.NET site (C#) in Visual Studio.NET that is utlizing a grid view to pull data from a SQL DB. I am using the ASP.NET membership provider model for the website. Access to the site is only allowed with the correct username and password.

    I have a webform call Document Checklist. The webform is connect to a SQL table called document_checklist. I need the Document Checklist webform to display only those documents that the logged in user has not approved. I need for the user to click on the checkbox for those documents that he has approved and click the submit button to update the SQL DB. The SQL DB contains the following columns user_name, document_name, document_url date_added, date_approved and checkbox.

    I have been trying to work throught this and am not certain how to proceed.

    1. I am not certain how to query the SQL DB against my LoginName

    2. I do not now how to only display items checked.

    3. I do not now how to utilze checkbox to update the SQL DB.

    My aspx code is below: (I need assistance with the code behind)

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="document_checklist.aspx.cs" Inherits="members_document_checklist" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h2>DOCUMENT CHECKLIST</h2>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged2">
            <Columns>
                <asp:BoundField DataField="user_name" HeaderText="user_name" SortExpression="user_name" />
                <asp:TemplateField HeaderText="Document">
                    <ItemTemplate>
                        <asp:HyperLink ID="Document" NavigateUrl='<%# Eval("document_url") %>' Text='<%# Eval("document_name") %>' Target="_blank" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="document_type" HeaderText="document_type" SortExpression="document_type" />
                <asp:BoundField DataField="date_added" HeaderText="date_added" SortExpression="date_added" />
                <asp:TemplateField HeaderText="checkbox">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" 
                            AutoPostBack="true" 
                            OnCheckedChanged="chkSelect_CheckedChanged"/>
                    </ItemTemplate>
                </asp:TemplateField>
               </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bd_loginConnectionString3 %>"
            SelectCommand="SELECT [user_name], [document_name], [document_type], [date_added], [checkbox] FROM [document_checklist] WHERE ([checkbox] = @checkbox)">
                <SelectParameters>
                    <asp:Parameter DefaultValue='1' Name="checkbox" Type="Byte" />
                </SelectParameters>
    </asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" Text="Cancel" />
        <asp:Button ID="Button2" runat="server" Text="Submit" OnClick="Button2_Click" />
    </asp:Content>
    


     

    Thank you in advance for your help. I am new to this and really appreciate any assistance that you can provide.

    Allison

  • Re: Gridview, Checkbox and SQL Parameters base on LoginName

    11-04-2009, 3:02 AM
    Answer
    • Member
      92 point Member
    • bdhnavneet
    • Member since 07-11-2009, 3:36 AM
    • Pune
    • Posts 19

    hi,

    for your first answer here is the solution:

     Dim conn As System.Data.SqlClient.SqlConnection

    Dim cmd As System.Data.SqlClient.SqlCommand

     Dim cmdString As String =   "select [column name] from [table name] where (([column name] = @Username) and ([Password] = @Password))"

    conn = New SqlConnection("Data Source String")

    cmd = New SqlCommand(cmdString, conn)

    cmd.Parameters.Add(("@Username"), SqlDbType.Decimal, 9)

    cmd.Parameters("@Username").Value = TextBox.Text (textbox to compare with database)

    cmd.Parameters.Add(("@Password"), SqlDbType.Decimal, 9)

    cmd.Parameters("@Password").Value = TextBox.Text  (textbox to compare with database)

    conn.Open()

    Dim myReader As SqlDataReader

    myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

    If myReader.Read() Then FormsAuthentication.RedirectFromLoginPage(TextBox.Text, False)

    Else

    label.Text = "Invalid Id or Password"

    End If

    myReader.Close()

    End Sub

    for the rest i will catch you later.

    first try this

    please mark the answer, as this will help others to find solution

    n.s.maurya
  • Re: Gridview, Checkbox and SQL Parameters base on LoginName

    11-04-2009, 8:29 PM
    Answer

    To your second question, I noticed that there's checkbox field in your database, and this field is of byte type. So You've got the code below:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bd_loginConnectionString3 %>" 
            SelectCommand="SELECT [user_name], [document_name], [document_type], [date_added], [checkbox] FROM [document_checklist] WHERE ([checkbox] = @checkbox)">  
                <SelectParameters>  
                    <asp:Parameter DefaultValue='1' Name="checkbox" Type="Byte" />  
       </SelectParameters>
    ......

    This has helped you to choose all kinds of records whose checkbox is true now. You don't need to do any other things more...

    As to your third one, I think you may double-click your Checkbox in your template, and define your DataKeyNames for the GridView in the property Panel. At last, write code like this below:

    Please change your ItemTemplate like this below:
    <asp:TemplateField HeaderText="checkbox">  
                    <ItemTemplate>  
                        <asp:CheckBox ID="chkSelect" runat="server"
          AutoPostBack=true Checked='<%#Eval("checkbox").ToString()=="1"%>'
                            OnCheckedChanged="chkSelect_CheckedChanged"/>  
                    </ItemTemplate>  
    </asp:TemplateField>
     
    private void CheckBox_checked(object sender, EventArgs e)
    {
     CheckBox chk = (CheckBox)sender;
     using (SqlCommand cmd = new SqlCommand())
     {
      cmd.Connection = new SqlConnection("....");
      cmd.CommandText = "Update XXX set [checkbox]="+ chk.Checked + "where [id]="+ GridView1.DataKeyNames[(chk.NamingContainer as GridViewRow).rowIndex].Value].ToString();
      cmd.ExecuteNonQuery();
      
      //Re-databind your DataTable to your GridView again now...
     }
     
    }

    I'll be very glad to be here to answer different kinds of questions though I'm new here. And please mark the answer as "Answered" --no matter who made the answer to you. Let's hand in hand to create a wonderful world of coding!


    Lanugate Conversion: http://www.developerfusion.com/tools/convert/csharp-to-vb/
  • Re: Gridview, Checkbox and SQL Parameters base on LoginName

    11-05-2009, 1:50 PM
    • Member
      3 point Member
    • Allison
    • Member since 09-11-2009, 12:29 AM
    • Posts 9

    Decker Dong,

    Thank you so much for your help. I an working through this code now. I have a few compile issues, but think that this will do the trick.

    Thanks again,

     

    Allison

  • Re: Gridview, Checkbox and SQL Parameters base on LoginName

    11-05-2009, 1:51 PM
    • Member
      3 point Member
    • Allison
    • Member since 09-11-2009, 12:29 AM
    • Posts 9

    Hello bdhnavneet,

    Thank you so much for your help. I am having some issues with the code you provided to display datafrom the sql db based on the login name (from .net). I will try to work through it to see if I can get this to work.

    Thanks again,

    Allison 

  • Re: Gridview, Checkbox and SQL Parameters base on LoginName

    11-07-2009, 3:40 PM
    • Member
      3 point Member
    • Allison
    • Member since 09-11-2009, 12:29 AM
    • Posts 9

    Hello bdhnavneet,

    Thanks again, I worked through the code again and it now makes sense.

    Allison

Page 1 of 1 (6 items)