How to check if LOGON USER already answered

两盒软妹~` 提交于 2019-12-08 06:35:30

问题


In my code I have this

<h2> Address Book</h2> <br />
    <table>
    <tr><td><b>User:</b></td><td><asp:Label ID="useren" runat="server"></asp:Label></td></tr> 
    <tr><td><b>Business:</b></td><td><asp:TextBox ID="business" runat="server" MaxLength="13"></asp:TextBox> (Example: 234925xxx)</td></tr>
    <tr><td><b>Business 2:</b></td><td><asp:TextBox ID="business2" runat="server" MaxLength="13"></asp:TextBox> (Example: xxxx)</td></tr>
    <tr><td><b>Mobile:</b></td><td><asp:TextBox ID="mobile" runat="server" MaxLength="13"></asp:TextBox> (Example: 9xxxxxxxx)</td></tr>
    <tr><td colspan="2"><asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label></td></tr>
    <tr><td><asp:Button ID="Button1" runat="server" Text="Save" onclick="cmdSave_Click2" /></td><td></td></tr>
    </table

In my code behind this to retrieve the logon user

protected void Page_Load(object sender, EventArgs e)
{
    useren.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
}

This to storage the text box and the logon user into the database

protected void cmdSave_Click2(object sender, EventArgs e)
{
    string sFilePath = Server.MapPath("Database3.accdb");
    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
    string insertCmd = "INSERT INTO Worker(Business,Business2,Mobile,username) VALUES (@Business,@Business2,@Mobile,@useren)";
    using (Conn)
    {
        Conn.Open();
        OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
        myCommand.Parameters.AddWithValue("@Business", business.Text);
        myCommand.Parameters.AddWithValue("@Business2", business2.Text);
        myCommand.Parameters.AddWithValue("@Mobile", mobile.Text);
        myCommand.Parameters.AddWithValue("@useren", HttpContext.Current.User.Identity.Name); 
        myCommand.ExecuteNonQuery(); 
        Label1.Text = "Saved Successfull!";
        Label1.ForeColor = System.Drawing.Color.Green;
    }
}

How can I check if the logon user already answered? My goal is if the logon user already exist em Access database retrieve his data or how can I make when I execute the button onclick="cmdSave_Click2" if the user exist retrive an label warning up "the user already answered" for example


回答1:


Give an attempt in this way. It may give you a idea to solve the issue.

protected void cmdSave_Click2(object sender, EventArgs e)
{
  string sFilePath = Server.MapPath("Database3.accdb");
  OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
  con.Open();
  OleDbCommand cmd1 = new OleDbCommand("Select count(*) from Worker where username = '"+HttpContext.Current.User.Identity.Name+"'",con);
  int total = (Int32)cmd1.ExecuteScalar();
  if(total==0)
  {
   OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
  string insertCmd = "INSERT INTO Worker(Business,Business2,Mobile,username) VALUES (@Business,@Business2,@Mobile,@useren)";
  using (Conn)
  {
    Conn.Open();
    OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
    myCommand.Parameters.AddWithValue("@Business", business.Text);
    myCommand.Parameters.AddWithValue("@Business2", business2.Text);
    myCommand.Parameters.AddWithValue("@Mobile", mobile.Text);
    myCommand.Parameters.AddWithValue("@useren", HttpContext.Current.User.Identity.Name); 
    myCommand.ExecuteNonQuery(); 
    Label1.Text = "Saved Successfull!";
    Label1.ForeColor = System.Drawing.Color.Green;
    Conn.Close();
  }
  }
  else
  {
       Label1.Text = "Existing user";
  }
  con.Close();
}


来源:https://stackoverflow.com/questions/31406841/how-to-check-if-logon-user-already-answered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!