如何在单击中在两个表中插入数据,其中第一个查询ID在另一个表中使用

时间:2014-03-05 11:23:36

标签: c# sql

更新

protected void submit()
{
    int pptId = 0;
    try
    {
        con.Open();
        da = new SqlDataAdapter();
        dt = new DataTable();
        string name = tbPPTName.Text;
        string strSQL = "SELECT * FROM ppt_Master WHERE ppt_Name='@ppt_Name'";
        da.SelectCommand = new SqlCommand(strSQL, con);
        da.Fill(dt);

        if (dt.Rows.Count > 0) // Means first name is already present
        {
            lblmsg.Text = "This ppt is already added!";
        }
        else if (dt.Rows.Count == 0)
        {
            lblmsg.Visible = false;
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "Insert into ppt_Master(Userid,ppt_Name,ModuleId,Description,Priority,IsEnable,dt) OUTPUT INSERTED.pptId values('" + Session["trainer"] + "','" + tbPPTName.Text + "','" + ddlModule.SelectedValue + "','" + tbDescription.Text + "','" + lblPriority.Text + "','" + chkIsEnable.Checked + "','" + DateTime.Now + "')";
                cmd.Parameters.AddWithValue("@ppt_Name", tbPPTName.Text.Trim());
                cmd.Parameters.AddWithValue("@ModuleId", ddlModule.SelectedItem.Text.Trim());//
                cmd.Parameters.AddWithValue("@Description", tbDescription.Text.Trim());
                cmd.Parameters.AddWithValue("@Priority", lblPriority.Text.Trim());
                cmd.Parameters.AddWithValue("@IsEnable", true);
                cmd.Parameters.AddWithValue("@dt", DateTime.Now.ToString());
                pptId = (Int32)cmd.ExecuteScalar();
             }
            tbPPTName.Text = "";
            tbDescription.Text = "";
            tbPPTName.Focus();
            Logs.InsertLogs(Session["Role"].ToString() + ":" + Session["trainer"].ToString(), "CreatePPT.aspx.cs submit()", "Query Successfully Executed for submit()");
            lblmsg.Text = "PPT Created";
        }
    }
    catch (Exception ex2)
    {

    }
    finally
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }            
    }

    #region content
    ViewState["pptId"] = pptId.ToString();
    try
    {
        con.Open();
        string path = "http://Userlogin.aspx?pptId=" + pptId.ToString();            
        using (SqlCommand cmd = con.CreateCommand())
        {
            cmd.CommandText = "Insert into windowformsppt(ContentName,ContentPath,Contenturl,Enableurl,dt) values ('" + tbPPTName.Text.ToString() + "','c:\t.exe' ,'" + path + "','No'" + ",'" + DateTime.Now + "')";
            cmd.Parameters.AddWithValue("@ContentName", "Assessment " +tbPPTName.Text.Trim());
            cmd.Parameters.AddWithValue("@Contentpath", "c:\t.exe");
            cmd.Parameters.AddWithValue("@Contenturl", "http://fpcbt/Userlogin.aspx?pptId=" + pptId.ToString());
            cmd.Parameters.AddWithValue("@dt", DateTime.Now);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    catch (Exception excp)
    {

    }
    finally
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();

        }
    }
    #endregion
}
  

无法将对象从DBNull强制转换为其他类型。

如何解决此错误?我想要的是将创建的pptid收集到一个字符串中,然后将该字符串传递给内容网址。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我假设您使用SQL SERVER作为后端

尝试OUTPUT条款,即OUTPUT INSERTED.ID

cmd.CommandText = "Insert into ppt_Master(Userid,ppt_Name,ModuleId,Description,Priority,IsEnable,dt) OUTPUT INSERTED.ID values('" + Session["trainer"] + "','" + tbPPTName.Text + "','" + ddlModule.SelectedValue + "','" + tbDescription.Text + "','" + lblPriority.Text + "','" + chkIsEnable.Checked + "','" + DateTime.Now + "')";

然后使用ExecuteScalar

获取此插入的ID
int newId = (Int32) cmd.ExecuteScalar();