2011-12-03 9 views
0

私は新しいASP.NET開発者です。私はこのプログラミング言語で私に最初のWebアプリケーションを開発しています。 ウィザードのステップ1:には、管理者がユーザーのユーザー名を入力できるテキストボックスが含まれています。 をクリックし、次のボタンをクリックするとユーザー名が表示されます。データベースのusersテーブルに対してaをチェックします。彼がデータベースに存在する場合、彼の情報はWizard Step2に表示され、彼の情報は読み取り専用になります。彼が存在しない場合、管理者にメッセージが通知されます。ウィザードコントロールをカスタマイズする方法は?

ウィザードのステップ2:には、ユーザー情報を示すリピーターまたはプレースホルダーが含まれています。

ウィザードステップ3:また、ユーザーは、このステップは私のASP.NETコードが

彼の役割を編集するためのボタンを示すとともに、システム内のこのユーザの現在の役割が表示されます存在していた場合:

私はコードビハインド多くに苦しんでいます

using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class UserManagement : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 

     string username = TextBox1.Text; 

     string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 
     string cmdText = "SELECT * FROM employee WHERE Username = @Username"; 

     //For checking the user 
     if (username != null) 
     { 
      if (CheckUsername(username) == true) 
      { 
       try 
       { 
        SqlConnection conn = new SqlConnection(connString); 
        conn.Open(); 
        SqlDataReader myReader = null; 
        SqlCommand myCommand = new SqlCommand(cmdText, conn); 
        myReader = myCommand.ExecuteReader(); 
        while (myReader.Read()) 
        { 
         Console.WriteLine(myReader["Name"].ToString()); 
         Console.WriteLine(myReader["JobTitle"].ToString()); 
         Repeater1.DataSource = myReader; 
         Repeater1.DataBind(); 
         myReader.Close(); 
         conn.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.ToString()); 
       } 
      } 
     } 

     //For sending object to the Wizard1.PreRender 
     Wizard1.PreRender += new EventHandler(Wizard1_PreRender); 

    } 


    //Method for checking the existence of the username in the database (retrun true or false) 
    private bool CheckUsername(string username) 
    { 
     string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 
     string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'"; 

     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); // Open DB connection. 

      using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
      { 
       int count = (int)cmd.ExecuteScalar(); 
       // True (> 0) when the username exists, false (= 0) when the username does not exist. 
       return (count > 0); 
      } 
     } 

    } 


    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
    { 
     if (Wizard1.ActiveStepIndex == 1) 
     { 
      string username = TextBox1.Text; 
     } 
    } 


    //Method for replacing the default sidebar of the Wizard Control with a custom sidebar (represented in a repeater) 
    protected void Wizard1_PreRender(object sender, EventArgs e) 
    { 
     Repeater SideBarList = Wizard1.FindControl("HeaderContainer").FindControl("SideBarList") as Repeater; 
     SideBarList.DataSource = Wizard1.WizardSteps; 
     SideBarList.DataBind(); 
    } 

    protected string GetClassForWizardStep(object wizardStep) 
    { 
     WizardStep step = wizardStep as WizardStep; 

     if (step == null) 
     { 
      return ""; 
     } 
     int stepIndex = Wizard1.WizardSteps.IndexOf(step); 

     if (stepIndex < Wizard1.ActiveStepIndex) 
     { 
      return "prevStep"; 
     } 
     else if (stepIndex > Wizard1.ActiveStepIndex) 
     { 
      return "nextStep"; 
     } 
     else 
     { 
      return "currentStep"; 
     } 
    } 


    protected void Button1_Clicked(Object sender, EventArgs e) 
    { 
     // When the button is clicked, 
     // show the new role of the user 
     //Label1.Text = "...button clicked..."; 

    } 



} 



     //Session["Username"] = Username.Text; 
     //String strUserName = Request.QueryString["Username"]; 

     //string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 
     //string cmdText = "SELECT * FROM employee WHERE Username = @Username"; 

     ////For checking the user 
     //if (Request.QueryString["Username"] != null) 
     //{ 
     // //String strUserName = Request.QueryString["Username"]; 

     // ////Check userName Here 
     // //String strReturnStatus = "false"; 

     // if (CheckUsername(Request.QueryString["Username"]) == true) 
     // { 
     //  //strReturnStatus = "true"; 
     //  try 
     //  { 
     //   SqlConnection conn = new SqlConnection(connString); 
     //   conn.Open(); 
     //   SqlDataReader myReader = null; 
     //   SqlCommand myCommand = new SqlCommand(cmdText, conn); 
     //   myReader = myCommand.ExecuteReader(); 
     //   while (myReader.Read()) 
     //   { 
     //    Console.WriteLine(myReader["Name"].ToString()); 
     //    Console.WriteLine(myReader["JobTitle"].ToString()); 
     //    Repeater1.DataSource = myReader; 
     //    Repeater1.DataBind(); 
     //    myReader.Close(); 
     //    conn.Close(); 
     //   } 
     //  } 
     //  catch (Exception ex) 
     //  { 
     //   Console.WriteLine(ex.ToString()); 
     //  } 
     // } 

<asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="false" Width="80%" > 
      <WizardSteps> 
       <asp:WizardStep ID="WizardStep1" runat="server" title="Employee Username/Network ID"> 
        <table border="0"> 
         <tr> 
          <td class="InputLabel">Username:</td> 
          <td class="InputControl"> 
           <asp:TextBox ID="TextBox1" runat="server" /> 
          </td> 
         </tr> 
        </table> 
       </asp:WizardStep> 
       <asp:WizardStep ID="WizardStep2" runat="server" title="Manage User"> 
        <div class="content"> 
         <asp:Repeater ID="Repeater1" runat="server"> 
          <ItemTemplate> 

          </ItemTemplate> 
         </asp:Repeater> 
        </div> 
       </asp:WizardStep> 
       <asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role"> 
        <label for="role">Current Role: </label> 
        <asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" /> 
        <asp:RadioButtonList id="radio1" runat="server" TextAlign="left"> 
         <asp:ListItem id="option1" runat="server" value="Admin" /> 
         <asp:ListItem id="option2" runat="server" value="Contribute" /> 
         <asp:ListItem id="option3" runat="server" value="User" /> 
        </asp:RadioButtonList> 
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" /> 
       </asp:WizardStep> 
      </WizardSteps> 

      <HeaderTemplate> 
       <ul id="wizHeader"> 
        <asp:Repeater ID="SideBarList" runat="server"> 
         <ItemTemplate> 
          <li><a class="<%# GetClassForWizardStep(Container.DataItem) %>" title="<%#Eval("Name")%>"> 
           <%# Eval("Name")%></a> </li> 
         </ItemTemplate> 
        </asp:Repeater> 
       </ul> 
      </HeaderTemplate> 

     </asp:Wizard> 

とコードビハインドです。ユーザー名の確認でも機能しませんでした。理由はわかりません。また、データベースからユーザー情報を表示するために、コードを入れるかどうかはわかりません。


UPDATE:役割の

、私は役割を取得し、設定するための3つのテーブルがあります。

Userテーブル:名、ユーザー名、部門(ユーザ名が主キーである)

役割テーブル: RoleID、ロール名(RoleIDが主キーである)

それらの構造は以下の通りです

のUserRoleテーブル: UserRoleID、ユーザー名、RoleID(UserRoleIDが主キーである)


別の更新(LAST):

ユーザテーブル:名、ユーザー名、DepartmentCode(ユーザ名が主キーである)

Departmentテーブルのテーブル: DepartmentCode、DepartmantName(DepartmentCodeが主キーです)

ロールテーブル: RoleID、ロール名(RoleIDが主キーである)

のUserRoleテーブル: UserRoleID、ユーザー名、RoleID(UserRoleIDが主キーである)

私はWizard1_NextButtonClickメソッドで次のクエリを使用しています:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
    { 
     switch (Wizard1.WizardSteps[e.NextStepIndex].ID) 
     { 
      case "WizardStep2": 
       string username = TextBox1.Text; 
       string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 

       //For checking the user   
       if (!String.IsNullOrEmpty(username) && CheckUsername(username)) 
       { 
        try 
        { 
         Session["Username"] = username; 

         SqlConnection conn = new SqlConnection(connString); 
         conn.Open(); 
         //string cmdText = "SELECT * FROM employee WHERE Username = @Username"; 
         string cmdText = "SELECT dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo," + 
               "ISNULL(dbo.Roles.RoleID, 3) AS RoleID, dbo.Divisions.DivisionName" + 
             "FROM dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode" + 
               "LEFT OUTER JOIN dbo.Roles RIGHT OUTER JOIN dbo.UserRole ON dbo.Roles.RoleID = dbo.UserRole.RoleID ON" + 
               "dbo.employee.Username = dbo.UserRole.Username" + 
             "WHERE  (dbo.employee.Username = @Username)"; 
         SqlCommand myCommand = new SqlCommand(cmdText, conn); 
         myCommand.Parameters.AddWithValue("@Username", username); 
         DataTable table = new DataTable(); 
         SqlDataAdapter adapter = new SqlDataAdapter(myCommand); 
         adapter.Fill(table); 

         string Name = table.Rows[0]["Name"] as string; 
         string Username = table.Rows[0]["Username"] as string; 
         //string DivisionName = table.Rows[0]["DivisionName"] as string; 
         string JobTitle = table.Rows[0]["JobTitle"] as string; 
         string BadgeNo = table.Rows[0]["BadgeNo"].ToString(); 
         //string role = table.Rows[0]["RoleName"] as string; 

         lblName.Text = Name; 
         lblUsername.Text = Username; 
         //lblDivision.Text = DivisionName; 
         lblJobTitle.Text = JobTitle; 
         lblBadgeNo.Text = BadgeNo; 
         //lblRole.Text = role; 
        } 
        catch (Exception ex) 
        { 
         Console.WriteLine(ex.ToString()); 
        } 
       } 

       else 
       { 
        //If the user does not exist or a blank value has been entered 
        //Cancel the nextstep redirection and display an error message in a span 
        e.Cancel = true; 
        errorSpan.InnerText = "The user id specified is blank or does not exist"; 
       } 

       break; 
      case "WizardStep3": 

       //Simply bind the radio list 
       radio1.SelectedValue = lblRole.Text; 
       break; 
     } 
    } 

クエリは、私の名前、ユーザー名、部門が表示されます(または部門)、役職とバッジ番号ウィザードでステップ2。また、ロールを更新するのではなく、ロールを挿入および削除できるようにするウィザードのステップ3でユーザーのロールを表示する必要があります。

SQLServer Management Studioでクエリをテストしたところうまくいきましたが、C#コードに入れてもWebページに結果が表示されず、理由がわかりません。

+0

Waaaay too too information。ワンステップでウィザードを書いてみて、それがどうなるか教えてください。 – sq33G

答えて

1

1)私が発見した最初の問題は、ページロードのコードをWizard1_NextButtonClickイベントに移動しなければならないことでした。

2)リピーターのコードを削除し、代わりにステップ2で返された情報をラベルに配置することを選択しました。

3)ステップ3

4におけるユーザーの役割を更新するためのロジックを実装)は、HTMLソースに若干の変更をしたが、それ以外の場合は、最初に

ソースとそれを持っていたとほとんど同じです背後にあるコードを以下に表示されて、私は私のマシン上でテストしてみて、あなたはすべての変更が必要な場合には、必要なものをやっているように見える私に知らせてくださいかかわら:

<asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="false" Width="80%" ActiveStepIndex="2" 
    OnNextButtonClick="Wizard1_NextButtonClick"> 
    <WizardSteps> 
     <asp:WizardStep ID="WizardStep1" runat="server" Title="Employee Username/Network ID"> 
      <table border="0"> 
       <tr> 
        <td class="InputLabel"> 
         Username: 
        </td> 
        <td class="InputControl"> 
         <asp:TextBox ID="TextBox1" runat="server" /> 
        </td> 
        <td> 
        <span id="errorSpan" runat="server" style="color:Red;"></span> 
        </td> 
       </tr> 
      </table> 
     </asp:WizardStep> 
     <asp:WizardStep ID="WizardStep2" runat="server" Title="Manage User"> 
      <div class="content"> 
       <table> 
        <tr> 
         <td> 
          <asp:Label ID="lblName" runat="server"></asp:Label> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <asp:Label ID="lblJobTitle" runat="server"></asp:Label> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <asp:Label ID="lblRole" runat="server"></asp:Label> 
         </td> 
        </tr> 
       </table> 
      </div> 
     </asp:WizardStep> 
     <asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role"> 
      <label for="role"> 
       Current Role: 
      </label> 
      <asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" /> 
      <asp:RadioButtonList ID="radio1" runat="server" TextAlign="left"> 
       <asp:ListItem id="option1" runat="server" Value="Admin" /> 
       <asp:ListItem id="option2" runat="server" Value="Contribute" /> 
       <asp:ListItem id="option3" runat="server" Value="User" /> 
      </asp:RadioButtonList> 
      <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" /> 
      <span id="infoSpan" runat="server" style="color:Red;"></span> 
     </asp:WizardStep> 
    </WizardSteps> 
    <HeaderTemplate> 
     <ul id="wizHeader"> 
      <asp:Repeater ID="SideBarList" runat="server"> 
       <ItemTemplate> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ul> 
    </HeaderTemplate> 
</asp:Wizard> 


using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //Set the Wizard Step 0 as the initial wizard step when the page loads 
      if (!Page.IsPostBack) 
      { 
       Wizard1.ActiveStepIndex = 0; 
      } 
     } 

     protected void Button1_Clicked(object sender, EventArgs e) 
     { 
      //If one of the items is selected AND a username exists in the Username session object update the user role 
      if (!String.IsNullOrEmpty(radio1.SelectedValue) && Session["Username"] != null) 
      { 
       string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 
       string cmdText = "UPDATE employee SET Role = '" + radio1.SelectedValue + "'" + 
        "WHERE Username = '" + Session["Username"].ToString() + "'"; 
       using (SqlConnection conn = new SqlConnection(connString)) 
       { 
        conn.Open(); 
        using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
        { 
         cmd.ExecuteScalar(); 
         infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue); 
        } 
       } 
      } 
     } 

     //Method for checking the existence of the username in the database (retrun true or false) 
     private bool CheckUsername(string username) 
     { 
      string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 
      string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'"; 
      using (SqlConnection conn = new SqlConnection(connString)) 
      { 
       conn.Open(); 
       // Open DB connection. 
       using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
       { 
        int count = (int)cmd.ExecuteScalar(); 
        // True (> 0) when the username exists, false (= 0) when the username does not exist. 
        return (count > 0); 
       } 
      } 
     } 

     protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
     { 
      switch (Wizard1.WizardSteps[e.NextStepIndex].ID) 
      { 
       case "WizardStep2": 
        string username = TextBox1.Text; 
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 

        //For checking the user   
        if (!String.IsNullOrEmpty(username) && CheckUsername(username)) 
        { 
         try 
         { 
          Session["Username"] = username; 

          SqlConnection conn = new SqlConnection(connString); 
          conn.Open(); 
          string cmdText = "SELECT * FROM employee WHERE Username = @Username"; 
          SqlCommand myCommand = new SqlCommand(cmdText, conn); 
          myCommand.Parameters.AddWithValue("@Username", username); 
          DataTable table = new DataTable(); 
          SqlDataAdapter adapter = new SqlDataAdapter(myCommand); 
          adapter.Fill(table); 

          string name = table.Rows[0]["Name"] as string; 
          string jobtitle = table.Rows[0]["JobTitle"] as string; 
          string role = table.Rows[0]["Role"] as string; 

          lblName.Text = name; 
          lblJobTitle.Text = jobtitle; 
          lblRole.Text = role; 
         } 
         catch (Exception ex) 
         { 
          Console.WriteLine(ex.ToString()); 
         } 
        } 

        else 
        { 
         //If the user does not exist or a blank value has been entered 
         //Cancel the nextstep redirection and display an error message in a span 
         e.Cancel = true; 
         errorSpan.InnerText = "The user id specified is blank or does not exist"; 
        } 

        break; 
       case "WizardStep3": 
        //Simply bind the radio list 
        radio1.SelectedValue = lblRole.Text; 
        break; 
      } 
     } 
    } 
} 

TIP: Storeで、あなたの接続文字列ウェブ。設定:

<connectionStrings> 
    <add name="conn" connectionString="Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"/> 
    </connectionStrings> 

はその後コードで、それはそうのようにアクセスすることができます。

string connString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 
+0

ありがとうございました。あなたのコードはとても素敵で素晴らしいです。あなたは本当に偉大な仕事をし、あなたは人生を救う。私は上記のすべての作業をした後でもっと尋ねるべきではないことを知っていますが、コードに追加しようとしたことであなたの助けが必要ですが、何度も失敗しました。私が望むのは、別のテーブルに設定されているユーザーロールを表示し、管理者にこのロールを変更する権限を与えることです。私はあなたが上でそれをしたことを知っていますが、その役割はユーザー情報の同じテーブルにあると仮定しました。上記の質問の最後の編集をご覧ください。 – user976711

1

はちょうどあなたのコメントに応答します。

私は、コードを調整する方法についてかなり良い考えがあると思います。基本的には、必要な情報を取得するためにいくつかの内部結合を実行し、セッション変数にロールIDを格納してウィザードのステップ3に

ただ、いくつかのことを可能に更新する私は、選択クエリが(TOPは、(1))、あなたはそれに応じて、多分持ってこれを変更することができる唯一の最初の結果を返します)

1を注意したいのですがdbから返された結果が複数ある可能性がある場合は、リピータコントロールを返す

2)インラインではあまり柔軟ではありませんコード内のクエリは、それらをストアドプロシージャに移動することをお勧めします(この方法については、Web上のサンプルのaloooootがあります)

3)ラジオボタンリストオプションをハードコードする代わりに、あなたのサイトを再配備する必要なく\ロールを簡単に追加できます。

<asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="false" Width="80%" ActiveStepIndex="2" 
    OnNextButtonClick="Wizard1_NextButtonClick"> 
    <WizardSteps> 
     <asp:WizardStep ID="WizardStep1" runat="server" Title="Employee Username/Network ID"> 
      <table border="0"> 
       <tr> 
        <td class="InputLabel"> 
         Username: 
        </td> 
        <td class="InputControl"> 
         <asp:TextBox ID="TextBox1" runat="server" /> 
        </td> 
        <td> 
        <span id="errorSpan" runat="server" style="color:Red;"></span> 
        </td> 
       </tr> 
      </table> 
     </asp:WizardStep> 
     <asp:WizardStep ID="WizardStep2" runat="server" Title="Manage User"> 
      <div class="content"> 
       <table> 
        <tr> 
         <td> 
          <asp:Label ID="lblName" runat="server"></asp:Label> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <asp:Label ID="lblDepartment" runat="server"></asp:Label> 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <asp:Label ID="lblRole" runat="server"></asp:Label> 
         </td> 
        </tr> 
       </table> 
      </div> 
     </asp:WizardStep> 
     <asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role"> 
      <label for="role"> 
       Current Role: 
      </label> 
      <asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" /> 
      <asp:RadioButtonList ID="radio1" runat="server" TextAlign="left"> 
       <asp:ListItem id="option1" runat="server" Value="1" Text="Admin" /> 
       <asp:ListItem id="option2" runat="server" Value="2" Text="Contribute" /> 
       <asp:ListItem id="option3" runat="server" Value="3" Text="User" /> 
      </asp:RadioButtonList> 
      <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" /> 
      <span id="infoSpan" runat="server" style="color:Red;"></span> 
     </asp:WizardStep> 
    </WizardSteps> 
    <HeaderTemplate> 
     <ul id="wizHeader"> 
      <asp:Repeater ID="SideBarList" runat="server"> 
       <ItemTemplate> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ul> 
    </HeaderTemplate> 
</asp:Wizard> 

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     private string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      //Set the Wizard Step 1 as the initial wizard step when the page loads 
      if (!Page.IsPostBack) 
      { 
       Wizard1.ActiveStepIndex = 0; 
      } 
     } 

     protected void Button1_Clicked(object sender, EventArgs e) 
     { 
      //If one of the items is selected AND a username exists in the Username session object update the user role 
      if (!String.IsNullOrEmpty(radio1.SelectedValue) && Session["Username"] != null) 
      { 
       string cmdText = "UPDATE Userrole SET RoleId = '" + radio1.SelectedValue + "'" + 
        "WHERE Username = '" + Session["Username"].ToString() + "'"; 
       using (SqlConnection conn = new SqlConnection(connString)) 
       { 
        conn.Open(); 
        using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
        { 
         cmd.ExecuteScalar(); 
         infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedItem.Text); 
        } 
       } 
      } 
     } 

     //Method for checking the existence of the username in the database (retrun true or false) 
     private bool CheckUsername(string username) 
     { 
      string cmdText = "SELECT Count(*) FROM users WHERE Username = '" + username + "'"; 
      using (SqlConnection conn = new SqlConnection(connString)) 
      { 
       conn.Open(); 
       // Open DB connection. 
       using (SqlCommand cmd = new SqlCommand(cmdText, conn)) 
       { 
        int count = (int)cmd.ExecuteScalar(); 
        // True (> 0) when the username exists, false (= 0) when the username does not exist. 
        return (count > 0); 
       } 
      } 
     } 

     protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) 
     { 
      switch (Wizard1.WizardSteps[e.NextStepIndex].ID) 
      { 
       case "WizardStep2": 
        string username = TextBox1.Text; 

        //For checking the user   
        if (!String.IsNullOrEmpty(username) && CheckUsername(username)) 
        { 
         try 
         { 
          Session["Username"] = username; 

          SqlConnection conn = new SqlConnection(connString); 
          conn.Open(); 
          //string cmdText = "SELECT FROM employee WHERE Username = @Username"; 
          string cmdText = "SELECT TOP(1) [Name],Department,RoleName,r.RoleId AS [RoleId] FROM users " + 
          "INNER JOIN userrole u on u.username = users.username " + 
          "INNER JOIN roles r on r.roleid = u.roleid " + 
          "WHERE users.username = @Username "; 

          SqlCommand myCommand = new SqlCommand(cmdText, conn); 
          myCommand.Parameters.AddWithValue("@Username", username); 
          DataTable table = new DataTable(); 
          SqlDataAdapter adapter = new SqlDataAdapter(myCommand); 
          adapter.Fill(table); 

          string name = table.Rows[0]["Name"] as string; 
          string department = table.Rows[0]["Department"] as string; 
          string role = table.Rows[0]["RoleName"] as string; 
          Session["RoleId"] = table.Rows[0]["RoleId"]; 

          lblName.Text = name; 
          lblDepartment.Text = department; 
          lblRole.Text = role; 
         } 
         catch (Exception ex) 
         { 
          Console.WriteLine(ex.ToString()); 
         } 
        } 

        else 
        { 
         //If the user does not exist or a blank value has been entered 
         //Cancel the nextstep redirection and display an error message in a span 
         e.Cancel = true; 
         errorSpan.InnerText = "The user id specified is blank or does not exist"; 
        } 

        break; 
       case "WizardStep3": 
        //Simply bind the radio list if the list contains the role retrieved 
        var roleId = Session["RoleId"]; 
        if (roleId != null && radio1.Items.FindByValue(roleId.ToString()) != null) 
        { 
         radio1.SelectedValue = Session["RoleId"].ToString(); 
        } 
        break; 
      } 
     } 
    } 
} 
+0

もう一度あなたはとてもいい仕事をしました。特にロールの問題で私の人生を楽にさせてくれます。しかし、私はまだC#コードでSQLクエリに問題があります。私はあなたのものを使用しませんでした。なぜなら私のデータベース設計は、あなたが使ったものとは異なるからです。あなたがこの問題で私を助けることができるかどうかはわかりません。上記の質問を更新して、私が持っている問題について考えてみましょう。 – user976711

関連する問題