I've two tables company with CompanyID {auto increment primary key} and group with GroupID {auto increment primary key}. In the group table I want to insert group details on submit button click along with updating company table's groups column with the GroupID just inserted in group table. Please suggest me how should I do this either with separate commands or through stored procedure. I'm little bit weak with SQL server queries so need some help with explanation. What am I doing wrong it gives "Incorrect syntax near the keyword 'inner'". Thanks in advance.
Submit button click code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string group = GroupName.Text;
string dept = Department.Text;
string company = Session["com"].ToString();
if ((HttpContext.Current.User != null) && HttpContext.Current.User.Identity.IsAuthenticated && company != null)
{
string strcon = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
con.Open();
DataSet ds = new DataSet();
SqlDataReader rdr = null;
SqlCommand cmd1 = new SqlCommand("Insert into Group(GroupName, Department, CompanyID) Values(@groupName, @department, 'Select CompanyID from Company1 where Company.CompanyName=@companyName')", con);
SqlCommand cmd = new SqlCommand("Update Company1 set Company1.Groups=Group.GroupID inner join Group on Group.CompanyID=Comapny1.CompanyID"
+" where Company.CompanyName=@companyName", con);
cmd1.Parameters.Add("@groupName", SqlDbType.NVarChar).Value = group;
cmd1.Parameters.Add("@department", SqlDbType.NVarChar).Value = dept;
cmd1.Parameters.Add("@companyName", SqlDbType.NVarChar).Value = company;
cmd.Parameters.Add("@companyName", SqlDbType.NVarChar).Value = company;
//cmd1.Parameters.AddWithValue("@groupName", group);
//cmd1.Parameters.AddWithValue("@department", dept);
//cmd.Parameters.AddWithValue("@companyName", company);
rdr = cmd1.ExecuteReader();
rdr = cmd.ExecuteReader();
con.Close();
}
else
{
Response.Redirect("Account/Login.aspx");
}
}