C# 如何在第一个查询id在另一个表中使用的地方,一次单击就在两个表中插入数据
更新C# 如何在第一个查询id在另一个表中使用的地方,一次单击就在两个表中插入数据,c#,sql,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=
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
收集到一个字符串中,然后将该字符串传递到内容url中。我该怎么做呢?我假设您使用SQL SERVER作为后端
Try子句,即插入的输出.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 + "')";
然后使用
不同的RDBMS有不同的方法获取insert语句创建的标识值。您正在使用哪些RDBMS?MySQL、SQL Server、Oracle、PosGreSQL等?采用SQL服务器;这应该在回答中说明。@NitinVarpe你能告诉我如何准确地使用用户代码吗?因为我的大脑现在不工作了,但我完全迷路了,因为我使用大脑lot@AmiteshExecuteScalar返回最后一个查询的第一行的第一列,在我们的情况下,它将是Id,因为我们已经从中返回了输出INSERTED.Idquery@Amitesh你需要甚至不需要使用OUTPUT INSERTED.ID,所以当我使用cmd.commedtext和int newid时,只要使用ExecuteScalar就可以了。我的数据在ppt_主表中插入了两次
int newId = (Int32) cmd.ExecuteScalar();