使用relatinship在数据库中插入数据的问题 [英] problem in inserting the data in database using relatinship

查看:96
本文介绍了使用relatinship在数据库中插入数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



i我的项目中有一个问题。



在一个页面中创建..

hello.

i hv one problem in my project.

created in one page ..

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
     <asp:Panel ID="Panel2" runat="server">
        <table cellpadding="5" cellspacing="5" style="color: Navy; font-size: 10pt; width: 50%;<br mode=" hold=" />            border: 1px solid #000000" align="center">
            <tr>
                <td colspan="2" align="left">
                    <asp:Label ID="lbladmin" runat="server" Font-Bold="True" Font-Size="X-Large" ForeColor="Black"

                        Text="E-Anatomy Admin Panel">
                </td>
            </tr>
            <tr>
                <td style="text-align: center;" colspan="2">
                     
                </td>
            </tr>
            <tr>
                <td align="right">
                     
                </td>
                <td align="left">
                     
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <table align="center">
                        <tr>
                            <td style="text-align: left;" colspan="3" class="style1">
                                <asp:Label ID="lblanatomydtails" runat="server" Font-Bold="True" Font-Size="Large"

                                    ForeColor="Black" Text="Anatomy Details">
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: left; width: 35%;">
                                 
                            </td>
                            <td style="text-align: left; width: 45%;">
                                 
                            </td>
                            <td style="text-align: left; width: 12%;">
                                <%--<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                            ControlToValidate="TxtUserName" ValidationGroup="vg">*--%> 
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Panel ID="Panel4" runat="server">
                                    <table>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Select   anatomy :
                                            </td>
                                            <td style="text-align: left; width: 45%;">
                                                <asp:DropDownList ID="ddlanatomyCate" runat="server" Width="150px" 

                                                    AutoPostBack="True" 

                                                   >
                                                
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                <%--<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                            ControlToValidate="TxtUserName" ValidationGroup="vg">*--%>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Upload Image Icon :
                                            </td>
                                            <td style="text-align: left; width: 45%;">
                                                <asp:FileUpload ID="iconuplaod" runat="server" />
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                    </table>
                                
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <asp:Panel ID="Panel1" runat="server">
                                
                                    <table>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Tital:
                                            </td>
                                            <td style="text-align: left; width: 45%;">
                                                <asp:TextBox ID="txttital1" runat="server" Font-Bold="True" Font-Size="Large" Width="200px">
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Upload the Screen Shot1:
                                            </td>
                                            <td align="left">
                                                <asp:FileUpload ID="ScreenShot1" runat="server" />
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Description:
                                            </td>
                                            <td align="left">
                                                <asp:TextBox ID="txtdesc1" runat="server" BorderColor="#000099" BorderStyle="Solid"

                                                    BorderWidth="1px" Style="font-family: Tahoma; font-size: 10pt" TextMode="MultiLine"

                                                    Width="300px" Height="150px" >
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                 
                                            </td>
                                            <td align="left">
                                                 
                                                <asp:Button ID="btnSubmit1" runat="server" onclick="btnSubmit1_Click" 

                                                    Text="Submit" />
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                                <asp:LinkButton ID="lnkaddmore" runat="server" onclick="lnkaddmore_Click">AddMoreDetails
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                 
                                            </td>
                                            <td align="left">
                                                 
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                    </table>
                                
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Panel ID="Panel3" runat="server">
                                    <table>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Tital:
                                            </td>
                                            <td style="text-align: left; width: 45%;">
                                                <asp:TextBox ID="txttiltel2" runat="server" Font-Bold="True" Font-Size="Large" Width="200px">
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Upload the Screen Shot1:
                                            </td>
                                            <td align="left">
                                                <asp:FileUpload ID="Screenshot2" runat="server" />
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                Description:
                                            </td>
                                            <td align="left">
                                                <asp:TextBox ID="txtdesc2" runat="server" BorderColor="#000099" BorderStyle="Solid"

                                                    BorderWidth="1px" Style="font-family: Tahoma; font-size: 10pt" TextMode="MultiLine"

                                                    Width="300px" Height="150px" >
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                 
                                            </td>
                                            <td align="left">
                                                 
                                                <asp:Button ID="btnSubmit2" runat="server" onclick="btnSubmit2_Click" 

                                                    Text="Submit" />
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                                <asp:LinkButton ID="lnkaddmore1" runat="server">AddMoreDetails
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align: left; width: 35%;">
                                                 
                                            </td>
                                            <td align="left">
                                                 
                                            </td>
                                            <td style="text-align: left; width: 12%;">
                                                 
                                            </td>
                                        </tr>
                                    </table>
                                
                            </td>
                        </tr>
                        
                    </table>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                     
                </td>
            </tr>
        </table>
    
    
    </div>
    </form>
</body>
</html>



code-
$b $b


code-

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Panel3.Visible = false;
            Bind();
            BindDataList();
        }

    }



    protected void BindDataList()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
        }
        //dtlist.DataSource = listItems;
        //dtlist.DataBind();

    }

    protected void Bind()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from EanatomyCategory", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        ddlanatomyCate.DataSource = dt;
        ddlanatomyCate.DataTextField = "AnatomyName";
        ddlanatomyCate.DataValueField = "ID";
        ddlanatomyCate.DataBind();
        //ddlanatomyCate.Items.Insert(0, new ListItem("-Select Anatommy-", "0"));
    }
    protected void btnSubmit1_Click(object sender, EventArgs e)
    {
        String extention;
        String name = iconuplaod.FileName;
        string filename2 = Path.GetFileName(ScreenShot1.PostedFile.FileName);
        String name1 = name.ToUpper();

        if (name1.EndsWith(".JPG"))
            extention = ".JPG";
        else if (name1.EndsWith(".GIF"))
            extention = ".GIF";
        else if (name1.EndsWith(".JPEG"))
            extention = ".JPEG";
        else if (name1.EndsWith(".gif"))
            extention = ".gif";
        else
            extention = "";


        String filename = Path.GetFileName(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Today.Minute.ToString() + DateTime.Now.Second.ToString() + extention);

        iconuplaod.SaveAs(Server.MapPath("Images/" + filename));
        ScreenShot1.SaveAs(Server.MapPath("ScreenShot/" + filename2));

        int CatId = ddlanatomyCate.SelectedIndex;
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into AdminMaster(ID,A_icon,A_tital,A_screenshot1,A_desc1)values(@ID,@A_icon,@A_tital,@A_screenshot1,@A_desc1)", con);
        cmd.Parameters.AddWithValue("@ID", CatId);
        cmd.Parameters.AddWithValue("@A_icon", filename);
        
        cmd.Parameters.AddWithValue("@A_tital", txttital1.Text);
        cmd.Parameters.AddWithValue("@A_screenshot1", filename2);
        cmd.Parameters.AddWithValue("@A_desc1", txtdesc1.Text);

        cmd.ExecuteNonQuery();


        string targetPath = Server.MapPath("Images/" + filename);
        Stream strm = iconuplaod.PostedFile.InputStream;
        var targetFile = targetPath;
        //Based on scalefactor image size will vary
        GenerateThumbnails(0.5, strm, targetFile);
        //BindDataList();
        con.Close();
    }

    private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
    {
        using (var image = Image.FromStream(sourcePath))
        {
            var newWidth = (int)(160 * scaleFactor);
            var newHeight = (int)(160 * scaleFactor);
            var thumbnailImg = new Bitmap(newWidth, newHeight);
            var thumbGraph = Graphics.FromImage(thumbnailImg);
            thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbnailImg.Save(targetPath, image.RawFormat);
        }
    }
    protected void lnkaddmore_Click(object sender, EventArgs e)
    {
        Panel4.Visible = false;
        Panel1.Visible = false;
        Panel3.Visible = true;
    }
    protected void btnSubmit2_Click(object sender, EventArgs e)
    {
        string filename3 = Path.GetFileName(Screenshot2.PostedFile.FileName);

        Screenshot2.SaveAs(Server.MapPath("ScreenShot/" + filename3));


        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into MoreDetails(More_title,More_screenshot1,More_desc1)values(@More_title,@More_screenshot1,@More_desc1)", con);



        cmd.Parameters.AddWithValue("@More_title", txttiltel2.Text);
        cmd.Parameters.AddWithValue("@More_screenshot1", filename3);
        cmd.Parameters.AddWithValue("More_desc1", txtdesc2.Text);

        cmd.ExecuteNonQuery();
        con.Close();
    }
}



using database as below


using database as below

AdminMaster
-A_id
-ID
-A_icon
-A_tital
-A_Screenshot1
-A_desc1

EanatomyCategory
-ID
-AnatomyName

MoreDetails
-More_ID
-A_id
-ID
More_title
More_screeshot1
More_desc1



the problem is when i run default page and insert values the data is saved properly but when i click the more details then new panel is open with field title,imageuplaod and desc insert the value ,,the value will save under the selected anatomy and icon image....



if getting my point plz help me...



if problem in understanding the question then plz check the code above....



plzz


the problem is when i run default page and insert values the data is saved properly but when i click the more details then new panel is open with field title,imageuplaod and desc insert the value ,,the value will save under the selected anatomy and icon image....

if getting my point plz help me...

if problem in understanding the question then plz check the code above....

plzz

推荐答案

Your issue is that when inserting the More Info part, you aren’’t specifying the foreign key to link the record back to where it be longs. Specifically, you need to include A_ID and ID (and specify values for them) when inserting into MoreDetails.



As an aside, you are also missing the @ symbol in
Your issue is that when inserting the More Info part, you aren''t specifying the foreign key to link the record back to where it belongs. Specifically, you need to include A_ID and ID (and specify values for them) when inserting into MoreDetails.

As an aside, you are also missing the @ symbol in
cmd.Parameters.AddWithValue("More_desc1", txtdesc2.Text);


这篇关于使用relatinship在数据库中插入数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆