有关向数据库中添加数据和GridView显示数据有关问题
有关向数据库中添加数据和GridView显示数据问题
想在sql sever数据库中添加一条数据,然后立刻显示在GridView中,但是最后发现想向数据库中添加记录的话就不能显示在gridView中,能显示在gridView中就不能插入数据,想问怎么解决,谢谢各位。
这是能看GridView内容的时候添加数据:


然后我打开数据库连接之后是这样,就不能显示GridView内容:


------解决思路----------------------
数据库登录失败,检查下连接字符串、用户名密码
------解决思路----------------------
向SQL Server数据库添加数据在GridView中显示出来
//向数据库添加内容
<asp:TextBox ID="typename" runat="server" Font-Size="11pt"></asp:TextBox>
<asp:Button id="btmAdd" onclick="btmAdd_Click" runat="server" Font-Size="10pt" Text="添加"></asp:Button>//添加事件按钮
//GridView显示数据设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>//codego.net/tags/11/1/
//cs页面添加事件
protected void btmAdd_Click(object sender, EventArgs e)
{
ST_myConn.Open();
if (this.typename.Text != "")
{
string sqlstr1 = "select count(*) from ST_class where ST_c_name='" + typename.Text.ToString() + "'";
SqlCommand mycom = new SqlCommand(sqlstr1, ST_myConn);
int intcount = Convert.ToInt32(mycom.ExecuteScalar());
if (intcount > 0)
{
Response.Write("<script>alert('对不起!不允许添加相同记录!');location='BlogType.aspx'</script>");
}
else
{
string ST_sql = "insert into ST_class (ST_c_name,ST_date) values ('" + typename.Text.ToString() + "','" + DateTime.Now.ToString() + "')";
My_sqldata.ExceSQL(ST_sql);
Response.Write("<script>alert('添加成功!');location='BlogType.aspx'</script>");
}
}
}
想在sql sever数据库中添加一条数据,然后立刻显示在GridView中,但是最后发现想向数据库中添加记录的话就不能显示在gridView中,能显示在gridView中就不能插入数据,想问怎么解决,谢谢各位。
这是能看GridView内容的时候添加数据:
然后我打开数据库连接之后是这样,就不能显示GridView内容:
------解决思路----------------------
数据库登录失败,检查下连接字符串、用户名密码
------解决思路----------------------
向SQL Server数据库添加数据在GridView中显示出来
//向数据库添加内容
<asp:TextBox ID="typename" runat="server" Font-Size="11pt"></asp:TextBox>
<asp:Button id="btmAdd" onclick="btmAdd_Click" runat="server" Font-Size="10pt" Text="添加"></asp:Button>//添加事件按钮
//GridView显示数据设置
<asp:GridView ID="ClassList" runat="server" AutoGenerateColumns="False"
CellPadding="4" Font-Size="11pt" GridLines="None" Width="468px" OnRowCancelingEdit="ClassList_RowCancetingEdit" OnRowDeleting="ClassList_RowDeleting" OnRowEditing="ClassList_RowEditing" OnRowUpdating="ClassList_RowUpdating" ForeColor="#333333" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="ClassList_PageIndexChangging" PageSize="6" OnRowDataBound="ClassList_RowDataBound">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选项">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Height="1px" Width="1px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ST_c_id" HeaderText="类型编号" ReadOnly="True" />
<asp:BoundField DataField="ST_c_name" HeaderText="文章类别" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>//codego.net/tags/11/1/
//cs页面添加事件
protected void btmAdd_Click(object sender, EventArgs e)
{
ST_myConn.Open();
if (this.typename.Text != "")
{
string sqlstr1 = "select count(*) from ST_class where ST_c_name='" + typename.Text.ToString() + "'";
SqlCommand mycom = new SqlCommand(sqlstr1, ST_myConn);
int intcount = Convert.ToInt32(mycom.ExecuteScalar());
if (intcount > 0)
{
Response.Write("<script>alert('对不起!不允许添加相同记录!');location='BlogType.aspx'</script>");
}
else
{
string ST_sql = "insert into ST_class (ST_c_name,ST_date) values ('" + typename.Text.ToString() + "','" + DateTime.Now.ToString() + "')";
My_sqldata.ExceSQL(ST_sql);
Response.Write("<script>alert('添加成功!');location='BlogType.aspx'</script>");
}
}
}