Click here to Skip to main content
16,017,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void SignIn_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              if (isValid())
              {
                  SqlConnection conn = new SqlConnection(ConnectionString);
                  conn.Open();
                  SqlCommand cmd = new SqlCommand("add_profile", conn);
                  //cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.AddWithValue("@username", txtUsername.Text);
                  cmd.Parameters.AddWithValue("@password", txtpassword.Password);
                  cmd.Parameters.AddWithValue("@selectusertype", cmboSelectusertype.Text);
                 // conn.Open();
                  cmd.ExecuteNonQuery();
                  conn.Close();
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  da.Fill(dt);
                  if (dt.Rows.Count > 0)
                  {
                      refresh();
                      Employee emp = new Employee();
                      emp.ShowDialog();
                  }
              }
          }
          catch (SqlException ex)
          {
              MessageBox.Show(ex.Message);
          }

      }


What I have tried:

public bool isValid()
       {
           if (string.IsNullOrEmpty(this.txtUsername.Text) | string.IsNullOrEmpty(this.txtpassword.Password))
           {
               MessageBox.Show("provide User Name and Password");
           }

           else if (string.IsNullOrEmpty(cmboSelectusertype.Text))
           {
               MessageBox.Show("Select User Type");
           }
           return true;
       }
Posted
Updated 4-Sep-22 8:45am
Comments
[no name] 2-Sep-22 11:56am    
https://stackoverflow.com/questions/13816490/get-cell-value-from-a-datatable-in-c-sharp
Maciej Los 2-Sep-22 14:25pm    
Sounds like an answer :)

1 solution

You can use SqlCommand.ExecuteScalar Method (System.Data.SqlClient) | Microsoft Docs[^]

cmd.CommandText = "SELECT EmployeeId FROM dbo.Employees WHERE EmpName = @Name And EmpLastName = @LastName;";  
int empid = (int) cmd.ExecuteScalar();  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900