If username not exist “INSERT INTO” Else “UPDATE” (display info)

不打扰是莪最后的温柔 提交于 2019-12-12 03:19:17

问题


this is my table:

<table>
    <tr><td><b>Utilizador:</b></td><td><asp:Label ID="username" runat="server"></asp:Label></td></tr> 
    <tr><td><b>Telefone da Empresa:</b></td><td><asp:TextBox ID="empresa" runat="server" MaxLength="13"></asp:TextBox> (Exemplo: +351234925xxx)</td></tr>
    <tr><td><b>2º Telefone:</b></td><td><asp:TextBox ID="empresa2" runat="server" MaxLength="4"></asp:TextBox> (Exemplo: xxxx)</td></tr>
    <tr><td><b>Telemóvel:</b></td><td><asp:TextBox ID="telemovel" runat="server" MaxLength="13"></asp:TextBox> (Exemplo: +3519xxxxxxxx)</td></tr>
    <tr><td colspan="2"><asp:Label ID="lblInfo" runat="server" Font-Bold="True"></asp:Label></td></tr>
    <tr><td><asp:Button ID="cmdSave" runat="server" Text="Guardar" onclick="cmdSave_Click" /></td><td></td></tr>
</table>

i want verify IF username exist in database show me the answers to update ELSE insert into

this is my code behind so far. it's to insert into

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        username.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; 
    }

    protected void cmdSave_Click(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 colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)";
        using (Conn)
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            myCommand.Parameters.AddWithValue("@Empresa", empresa.Text);
            myCommand.Parameters.AddWithValue("@Empresa2", empresa2.Text);
            myCommand.Parameters.AddWithValue("@Telemovel", telemovel.Text);
            myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);            
            Response.Write(" ");
            myCommand.ExecuteNonQuery(); 
            lblInfo.Text = "Dados guardados!";
            lblInfo.ForeColor = System.Drawing.Color.Green;
        }
    }
}

my purpose is before answer my table show the answered questions [or display an message saying "User already answered"] if not exist in my database then display the table to field. It's driving me crazy can't to such thing


回答1:


This can be achieved like this,

SqlCommand objcmd = new SqlCommand("SELECT 1 from Table WHERE Username=@NAME" , objsqlconn);//get rows if username exists
cmd.Parameters.Add("@NAME", SqlDbType.NVarChar,20).Value = Master_product_txt.Text;   
objsqlconn.Open();
readr = SelectCommand.ExecuteReader();

if (!readr.HasRows) 
{
`// write Code to insert values
}`
else
{
    //write code to retrieve values and update          
}

Edited the code as pointed out by @ A ツ



来源:https://stackoverflow.com/questions/31519336/if-username-not-exist-insert-into-else-update-display-info

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