文本框值不会插入数据库中。 [英] Text box values do not insert in database.

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

问题描述

您好,

我在网格视图中添加了一个文本框&它的工作正常。但是当我将所有网格视图数据插入数据库时​​,除了文本框值之外,所有网格视图数据都插入到数据库中。现在请帮助我如何将所有数据(包括文本框值)插入我的数据库。

我的代码是::

 < span class =code-pagedirective><%@     Page    标题  =    语言  =  C#    MasterPageFile   = 〜/ Site.Master    AutoEventWireup   =  true    CodeBehind   =  Datacollection.aspx.cs   继承  =  New_NPS.Datacollection   %>  
< asp:内容 ID = Content1 ContentPlaceHolderID = HeadContent runat = server >
< / asp:Content >
< asp:内容 ID = Content2 ContentPlaceHolderID = 主要内容 runat = server >
< div >
< script typ e = text / javascript > ;
function isNumberKey(evt){
var charCode = (evt.which)? evt.which:event.keyCode
if (charCode> 31 &&( charCode< 48 || charCode> 57 ))
return false ;
return true ;
}

< / script >




< h1 > 把收款金额< / h1 >
< asp:GridView ID = GridView1 runat = server AllowPaging = True

AllowSorting = True AutoGenerateColumns = False DataKeyNames = Member_ID

DataSourceID = < span class =code-keyword> SqlDataSource1 CellPadding = 4 ForeColor = #3 33333

GridLines = style = margin-right:54px 宽度 = 303px

< span class =code-attribute> onselectedindexchanged = GridView1_SelectedIndexChanged >

< AlternatingRowStyle < span class =code-attribute> BackColor = 白色 / >

< >
< asp:BoundField < span class =code-attribute> DataField = Member_ID HeaderText = Member_ID ReadOnly = True

SortExpression = Member_ID / >
< asp:BoundField DataField = First_Name HeaderText = First_Name

< span class =code-attribute>
SortExpression = First_Name ReadOnly = True / >
< asp:BoundField DataField = Last_Name HeaderText = Last_Name

SortExpression = Last_Name ReadOnly = True / >


< asp:templatefield headertext = 收集金额 >
< itemtemplate >
< asp:textbox < span class =code-attribute> id = TextBox1 onkeypress = return isNumberKey(event) type = 文字 ReadOnly = False runat = server > < / asp:textbox >
< / itemtemplate >
< / asp:templatefield >










< / Columns >
< EditRowStyle BackColor = #2461BF / >
< FooterStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 / >
< 标题样式 BackColor = #507CD1 字体粗体 = True ForeColor = 白色 / >
< PagerStyle BackColor = #2461BF ForeColor = 白色 Horizo​​ntalAlign = 中心 / >
< RowStyle BackColor = #EFF3FB / >
< SelectedRowStyle BackColor = # D1DDF1 字体粗体 = ForeColor = #333333 / >
< SortedAscendingCellStyle BackColor = #F5F7FB / >
< SortedAscendingHeaderStyle BackColor = #6D95E1 / >
< SortedDescendingCellStyle BackColor = #E9EBEF / >
< SortedDescendingHeaderStyle < span class =code-attribute> BackColor = #4870BE / >
< / asp:GridView >
< asp:SqlDataSource ID = SqlDataSource1 runat = server

ConnectionString = <% $ ConnectionStrings:MemberConnectionString %>

< span class =code-attribute>

< span class =code-attribute> SelectCommand = SELECT [Member_Info]。[Member_ID],[Member_Info]。[First_Name],[Member_Info]。[Last_Name] FROM [Member_Info]内部联接[会员主持人] [Member_Info]。[Member_ID] = [会员]。[会员ID] > < ; / asp:SqlDataSource >



< asp:按钮 ID = btsave < span class =code-attribute> runat = server onclick = btsave_Click 文字 = 保存 / >



< / div >



< / asp:Content >





====================================

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;

命名空间 New_NPS
{
public partial class Datacollection:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}

受保护 void btsave_Click( object sender,EventArgs e)
{

string Member_ID = string .Empty;
// string Frist_Name = string.Empty;
// string Last_Name = string.Empty;
string TextBox1 = string .Empty;
// int collection = Convert.ToInt32(TextBox1);
// int TextBox1 = Convert.ToInt32(TextBox1.Text).ToString();
// collection = Convert.ToInt32(TextBox1.Text);



int GVCount = GridView1.Rows.Count;

foreach (GridViewRow GVRow GridView1.Rows)
{
Member_ID = GVRow.Cells [ 0 ]。文字;
// Frist_Name = GVRow.Cells [1] .Text;
< span class =code-comment> // Last_Name = GVRow.Cells [2] .Text;
TextBox1 = GVRow.Cells [ 3 ]。文字;

// int Mobile = int.Parse(tbmobileno.Text);


SqlConnection myConnection = new SqlConnection( user id = sa; +
password = prodip @km; server = PRODIP; +
Trusted_Connection = yes; +
database = Member;);
myConnection.Open();

SqlCommand myCommand = new SqlCommand( INSERT INTO [Collectionamt](Member_ID,collectionamt,collectiondate) +
值(@ Member_ID,@ TextBox1中,GETDATE()));

// int Member_ID = int.Parse(GVRow.Cells [1] .Text) ;


myCommand.Parameters.Add( @ Member_ID ,System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
myCommand.Parameters.Add( @ TextBox1,System.Data.SqlDbType.VarChar , 50 )。Value =(TextBox1.ToString());
// myCommand.Parameters.AddWithValue(@ TextBox1,TextBox1.ToString());

myCommand.Connection = myConnection;
myCommand.ExecuteNonQuery();

Response.Write( < script language ='javascript'> {alert( ' + 数据已保存')}< / script>);

myConnection.Close();

}
}

受保护 void GridView1_SelectedIndexChanged( object sender,EventArgs e)
{

}


}
}





..........请帮助;;;;;;;;; ;;;;;;;;;;

解决方案

ConnectionStrings:MemberConnectionString %>



SelectCommand=\"SELECT [Member_Info].[Member_ID], [Member_Info].[First_Name], [Member_Info].[Last_Name] FROM [Member_Info] inner join [Membermaster] on [Member_Info].[Member_ID]=[Membermaster].[Member_ID]\"></asp:SqlDataSource>



<asp:Button ID=\"btsave\" runat=\"server\" onclick=\"btsave_Click\" Text=\"Save\" />



</div>



</asp:Content>





====================================

using System; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;

namespace New_NPS
{
public partial class Datacollection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btsave_Click(object sender, EventArgs e)
{

string Member_ID = string.Empty;
//string Frist_Name = string.Empty;
//string Last_Name = string.Empty;
string TextBox1=string.Empty;
//int collection = Convert.ToInt32(TextBox1);
//int TextBox1 = Convert.ToInt32(TextBox1.Text).ToString();
//collection = Convert.ToInt32(TextBox1.Text);



int GVCount = GridView1.Rows.Count;

foreach (GridViewRow GVRow in GridView1.Rows)
{
Member_ID = GVRow.Cells[0].Text;
// Frist_Name = GVRow.Cells[1].Text;
//Last_Name = GVRow.Cells[2].Text;
TextBox1 = GVRow.Cells[3].Text;

//int Mobile = int.Parse(tbmobileno.Text);


SqlConnection myConnection = new SqlConnection(\"user id=sa;\" +
\"password=prodip@km;server=PRODIP;\" +
\"Trusted_Connection=yes;\" +
\"database=Member;\");
myConnection.Open();

SqlCommand myCommand = new SqlCommand(\"INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)\" +
\"Values(@Member_ID,@TextBox1,GETDATE())\");

//int Member_ID = int.Parse(GVRow.Cells[1].Text);


myCommand.Parameters.Add(\"@Member_ID\", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
myCommand.Parameters.Add(\"@TextBox1\", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());
//myCommand.Parameters.AddWithValue(\"@TextBox1\", TextBox1.ToString());

myCommand.Connection = myConnection;
myCommand.ExecuteNonQuery();

Response.Write(\"<script language='javascript'>{alert('\" + \"Data is Save')}</script>\");

myConnection.Close();

}
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}


}
}





..........Please help;;;;;;;;;;;;;;;;;;


TextBox1 is in your ItemTemplate, You need to find the TextBox to get it’’s value.

Replace your loop with this:

foreach (GridViewRow GVRow in GridView1.Rows) 
{
Member_ID = GVRow.Cells[0].Text;
TextBox1 = ((TextBox)GVRow.FindControl(\"TextBox1\")).Text;
SqlConnection myConnection = new SqlConnection(\"user id=sa;\" +
\"password=prodip@km;server=PRODIP;\" +
\"Trusted_Connection=yes;\" +
\"database=Member;\");
myConnection.Open();
SqlCommand myCommand = new SqlCommand(\"INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)\" +
\"Values(@Member_ID,@TextBox1,GETDATE())\");
myCommand.Parameters.Add(\"@Member_ID\", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
myCommand.Parameters.Add(\"@TextBox1\", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());
myCommand.Connection = myConnection;
myCommand.ExecuteNonQuery();
Response.Write(\"<script language=\"'javascript'\">{alert('\" + \"Data is Save')}</script>\");
myConnection.Close();
}









--Amit


Hello,
I add a text box in my grid view & its working fine. But when i insert all grid view data into database the all grid view data are inserted in database except text box values. Now please help me how i will insert all data (including Text box values) into my database.
My Code is::

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Datacollection.aspx.cs" Inherits="New_NPS.Datacollection" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
     <script type="text/javascript">
      function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }

   </script>




    <h1> put collection amount</h1>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"

            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Member_ID"

            DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"

            GridLines="None" style="margin-right: 54px" Width="303px"

            onselectedindexchanged="GridView1_SelectedIndexChanged">

                <AlternatingRowStyle BackColor="White" />

                <Columns>
                <asp:BoundField DataField="Member_ID" HeaderText="Member_ID" ReadOnly="True"

                        SortExpression="Member_ID" />
                    <asp:BoundField DataField="First_Name" HeaderText="First_Name"

                        SortExpression="First_Name" ReadOnly="True" />
                    <asp:BoundField DataField="Last_Name" HeaderText="Last_Name"

                        SortExpression="Last_Name" ReadOnly="True" />


                    <asp:templatefield headertext="Collection Amount">
                    <itemtemplate>
                    <asp:textbox id="TextBox1" onkeypress="return isNumberKey(event)" type="Text" ReadOnly="False"  runat="server"></asp:textbox>
                    </itemtemplate>
                    </asp:templatefield>










                        </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

                ConnectionString="<%$ ConnectionStrings:MemberConnectionString %>"



            SelectCommand="SELECT [Member_Info].[Member_ID], [Member_Info].[First_Name], [Member_Info].[Last_Name] FROM [Member_Info] inner join [Membermaster] on [Member_Info].[Member_ID]=[Membermaster].[Member_ID]"></asp:SqlDataSource>



          <asp:Button ID="btsave" runat="server" onclick="btsave_Click" Text="Save" />



          </div>



</asp:Content>



====================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace New_NPS
{
    public partial class Datacollection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btsave_Click(object sender, EventArgs e)
        {

            string Member_ID = string.Empty;
            //string Frist_Name = string.Empty;
            //string Last_Name = string.Empty;
           string TextBox1=string.Empty;
            //int collection = Convert.ToInt32(TextBox1);
            //int TextBox1 = Convert.ToInt32(TextBox1.Text).ToString();
            //collection = Convert.ToInt32(TextBox1.Text);



            int GVCount = GridView1.Rows.Count;

            foreach (GridViewRow GVRow in GridView1.Rows)
            {
                Member_ID = GVRow.Cells[0].Text;
                // Frist_Name = GVRow.Cells[1].Text;
                //Last_Name = GVRow.Cells[2].Text;
                TextBox1 = GVRow.Cells[3].Text;
                
                //int Mobile = int.Parse(tbmobileno.Text);


                SqlConnection myConnection = new SqlConnection("user id=sa;" +
                                           "password=prodip@km;server=PRODIP;" +
                                           "Trusted_Connection=yes;" +
                                           "database=Member;");
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand("INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)" +
    "Values(@Member_ID,@TextBox1,GETDATE())");

                //int Member_ID = int.Parse(GVRow.Cells[1].Text);


                myCommand.Parameters.Add("@Member_ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
                myCommand.Parameters.Add("@TextBox1", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());
                //myCommand.Parameters.AddWithValue("@TextBox1", TextBox1.ToString());
                
                myCommand.Connection = myConnection;
                myCommand.ExecuteNonQuery();

                Response.Write("<script language='javascript'>{alert('" + "Data is Save')}</script>");

                myConnection.Close();

            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
    }
}



..........Please help;;;;;;;;;;;;;;;;;;

解决方案

ConnectionStrings:MemberConnectionString %>" SelectCommand="SELECT [Member_Info].[Member_ID], [Member_Info].[First_Name], [Member_Info].[Last_Name] FROM [Member_Info] inner join [Membermaster] on [Member_Info].[Member_ID]=[Membermaster].[Member_ID]"></asp:SqlDataSource> <asp:Button ID="btsave" runat="server" onclick="btsave_Click" Text="Save" /> </div> </asp:Content>



====================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace New_NPS
{
    public partial class Datacollection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btsave_Click(object sender, EventArgs e)
        {

            string Member_ID = string.Empty;
            //string Frist_Name = string.Empty;
            //string Last_Name = string.Empty;
           string TextBox1=string.Empty;
            //int collection = Convert.ToInt32(TextBox1);
            //int TextBox1 = Convert.ToInt32(TextBox1.Text).ToString();
            //collection = Convert.ToInt32(TextBox1.Text);



            int GVCount = GridView1.Rows.Count;

            foreach (GridViewRow GVRow in GridView1.Rows)
            {
                Member_ID = GVRow.Cells[0].Text;
                // Frist_Name = GVRow.Cells[1].Text;
                //Last_Name = GVRow.Cells[2].Text;
                TextBox1 = GVRow.Cells[3].Text;
                
                //int Mobile = int.Parse(tbmobileno.Text);


                SqlConnection myConnection = new SqlConnection("user id=sa;" +
                                           "password=prodip@km;server=PRODIP;" +
                                           "Trusted_Connection=yes;" +
                                           "database=Member;");
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand("INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)" +
    "Values(@Member_ID,@TextBox1,GETDATE())");

                //int Member_ID = int.Parse(GVRow.Cells[1].Text);


                myCommand.Parameters.Add("@Member_ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
                myCommand.Parameters.Add("@TextBox1", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());
                //myCommand.Parameters.AddWithValue("@TextBox1", TextBox1.ToString());
                
                myCommand.Connection = myConnection;
                myCommand.ExecuteNonQuery();

                Response.Write("<script language='javascript'>{alert('" + "Data is Save')}</script>");

                myConnection.Close();

            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
    }
}



..........Please help;;;;;;;;;;;;;;;;;;


TextBox1 is in your ItemTemplate, You need to find the TextBox to get it''s value.
Replace your loop with this:

foreach (GridViewRow GVRow in GridView1.Rows)
{
    Member_ID = GVRow.Cells[0].Text;
    TextBox1 = ((TextBox)GVRow.FindControl("TextBox1")).Text;    
    SqlConnection myConnection = new SqlConnection("user id=sa;" +
                               "password=prodip@km;server=PRODIP;" +
                               "Trusted_Connection=yes;" +
                               "database=Member;");
    myConnection.Open();
    SqlCommand myCommand = new SqlCommand("INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)" +
"Values(@Member_ID,@TextBox1,GETDATE())");
    myCommand.Parameters.Add("@Member_ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
    myCommand.Parameters.Add("@TextBox1", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());    
    myCommand.Connection = myConnection;
    myCommand.ExecuteNonQuery();
    Response.Write("<script language="'javascript'">{alert('" + "Data is Save')}</script>");
    myConnection.Close();
}





--Amit


这篇关于文本框值不会插入数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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