c# - 从插入中获取 session - C# 和 MySQL

标签 c# mysql visual-studio session sql-insert

我以前从未这样做过,所以我希望你们中的一些人知道如何做到这一点。

本质上,我在 CreateModule 页面上进行插入,然后我想获取新的 ModuleID(这是在数据库中创建的,我尚未插入)和 ModuleTitle 并将其带到 CreateModule2 页面。

我非常感谢所提供的所有帮助。

C#

protected void CreateNewModule_Click(object sender, EventArgs e)
        {
            // open new connection
            SqlConnection connect1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            connect1.Open();

            // initalise variables for update
            String Title = ModuleTitleText.Text;
            String Mtext = ModuleTextText.Text;
            String Com = CompulsoryDropdown.Text;
            String CAT = CATpointsText.Text;
            String Lev = LevelText.Text;
            String Ass = AssessmentText.Text;
            String MCode = ModuleCodeText.Text;
            String Status = ModuleStatusDropdown.Text;

            // convert string to Int
            Int32 Levconverted = Convert.ToInt32(Lev);
            Int32 CATconverted = Convert.ToInt32(CAT);
            

            // Insert Query to Add new student record to student records table in database
            String queryInsert = "INSERT INTO Module_Info (ModuleTitle, ModuleText, Compulsory, CATpoints, Level, Assessment, ModuleCode, ModuleStatus) VALUES ('" + Title + "', '" + Mtext + "', '" + Com + "', '" + CAT + "', '" + Lev + "', '" + Ass + "',  '" + MCode + "', '" + Status + "'); SELECT  LAST_INSERT_ID()";

            // excute insert query
            SqlCommand myCommand = new SqlCommand(queryInsert, connect1);
            myCommand.Parameters.Add("@title", SqlDbType.NVarChar).Value = Title;
            int idmodule = Convert.ToInt32(myCommand.ExecuteScalar());
            
            

            // alerts for successfull upload
            Response.Write("<script type='text/javascript'>");
            Response.Write("alert('New Module has been added. Please select a course to align the module to in the next page.');");
            Response.Write("document.location.href='CreateModule2.aspx';");
            Response.Write("</script>");
        }

然后这是前端代码。

<table style="width: 100%;">
                <tr>
                    <td><asp:Label ID="ModuleTitle" runat="server" Text="Module Title" Font-Bold="true"></asp:Label></td>
                    <td><asp:TextBox ID="ModuleTitleText" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="1" width="700px" ></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="reqModuleTitle" controltovalidate="ModuleTitleText" 
                            errormessage="* Please enter the module title" ForeColor="Red" Font-Bold="true" Font-Size="Small" /></td>
                </tr>
                <tr>
                    <td><asp:Label ID="ModuleText" runat="server" Text="Module Text" Font-Bold="true" ></asp:Label></td>
                    <td><asp:TextBox ID="ModuleTextText" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="4" width="800px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="reqModuleText" controltovalidate="ModuleTextText" 
                            errormessage="*Please enter the Module Information" ForeColor="Red" Font-Bold="true" Font-Size="Small" /></td>
                </tr>
                <tr>
               <!-- dropdown list to select value-->
                  <td><asp:Label ID="Compulsory" runat="server" Text="Compulsory Status" Font-Bold="true" ></asp:Label> </td>
                    <td><asp:DropDownList ID="CompulsoryDropdown" runat="server">
                        <asp:ListItem Value="true">Compulsory</asp:ListItem>
                        <asp:ListItem Value="false">Non-Compulsory</asp:ListItem>
                </asp:DropDownList></td>
                </tr>
                <tr>
                    <td><asp:Label ID="CATpoints" runat="server" Text="CATpoints" Font-Bold="true"></asp:Label></td>
                    <td><asp:TextBox ID="CATpointsText" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="1" width="100px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="reqCATpoints" controltovalidate="CATpointsText" 
                            errormessage="*Please enter the A-Level Requirements" ForeColor="Red" Font-Bold="true" Font-Size="Small" /></td>
                </tr>
                <tr>
                    <td><asp:Label ID="Level" runat="server" Text="Level" Font-Bold="true"></asp:Label></td>
                    <td><asp:TextBox ID="LevelText" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="1" width="100px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="reqLevel" controltovalidate="LevelText" 
                            errormessage="*Please enter the Level of the module" ForeColor="Red" Font-Bold="true" Font-Size="Small" /></td>
                </tr>
                <tr>
                    <td><asp:Label ID="Assessment" runat="server" Text="Assessment" Font-Bold="true"></asp:Label></td>
                    <td><asp:TextBox ID="AssessmentText" runat="server" TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="1" width="600px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="ReqAssessment" controltovalidate="AssessmentText" 
                            errormessage="*Please enter the Assessment details" ForeColor="Red" Font-Bold="true" Font-Size="Small" /></td>
                </tr>
                <tr>
                    <td><asp:Label ID="ModuleCode" runat="server" Text="Module Code" Font-Bold="true" ></asp:Label>  </td>
                    <td><asp:TextBox ID="ModuleCodeText" runat="server"  TextMode="MultiLine" style="overflow:hidden" onkeyup="AutoExpand(this)" Rows="1" width="300px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" id="ReqModuleCode" controltovalidate="ModuleCodeText" 
                            errormessage="*Please enter the module code" ForeColor="Red" Font-Bold="true" Font-Size="Small" />
                    </td>
                </tr>
                <tr>
               <!-- dropdown list to select value-->
                  <td><asp:Label ID="ModuleStatus" runat="server" Text="Module Status" Font-Bold="true" ></asp:Label> </td>
                    <td><asp:DropDownList ID="ModuleStatusDropdown" runat="server">
                        <asp:ListItem Value="Running">Running</asp:ListItem>
                        <asp:ListItem Value="Suspended">Suspended</asp:ListItem>
                        <asp:ListItem Value="Withdrawn">Withdrawn</asp:ListItem>
                </asp:DropDownList></td>
            </tr>
               <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
               </tr>
               <tr>
                    <td><asp:Button ID="SubmitModule" runat="server" Text="Submit" OnClick="CreateNewModule_Click" /></td>
               </tr>
            </table>

最佳答案

您需要将其附加到 SQL 字符串的末尾:

SELECT SCOPE_IDENTITY()

这将返回新创建的记录的 ID。然后,您可以通过将 myCommand.ExecuteQuery() 替换为以下内容来获取该值:

int id = myCommand.ExecuteScalar();

然后,您可以使用带有 id 的 Response.Redirect 来访问下一页,并使用它从新创建的记录中加载任何内容。

关于c# - 从插入中获取 session - C# 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797796/

相关文章:

mysql - 使用 Join 删除孤立记录

mysql - 在 MySQL 中组合复合外键?

asp.net - 从命令行将文件包含在项目中

visual-studio - Web.config转换:未声明转换属性

c# - 如何在变量的值更改时调用方法?

c# - 在日历日打开文本框单击

c# - 为什么派生类中必须保护抽象方法?

javascript - 如何使用javascript清除asp.net复选框列表中的所有项目并添加新项目

php - 使用 PHP 和 MySQL 的基于步骤的审批系统

javascript - 在 Internet Explorer 上,未将对象引用设置为 AngularJS 中的对象实例