没有错误,但是没有使用网格将数据插入到db(mysql)中 [英] no error , but data is not inserting in to db(mysql) using grid

查看:86
本文介绍了没有错误,但是没有使用网格将数据插入到db(mysql)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web.config
< appSettings>
<添加key ="prtest" value ="DSN = MYSQL"/>
</appSettings>
< connectionStrings>
< add name ="ConnectionString" connectionString ="DSN = Mysql; uid = root; pwd = mysql" providerName ="System.Data.Odbc"></add>
</connectionStrings>
---------------
使用System.Data.Odbc;
使用System.Data;
使用System.Configuration;
公共局部类Default2:System.Web.UI.Page
{
OdbcConnection objcon =新的OdbcConnection(ConfigurationManager.AppSettings ["prtest"]);

受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!Page.IsPostBack)
{
loadgridview();
}}
私有void loadgridview()
{
字符串qry =从消息中选择*";
OdbcDataAdapter da =新的OdbcDataAdapter(qry,objcon);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
如果(e.CommandName =="InsertRecord")
{
-------------
var名称= GridView1.FooterRow.FindControl("txtname")作为TextBox;
var Email = GridView1.FooterRow.FindControl("txtemail")as TextBox;
var Message = GridView1.FooterRow.FindControl("txtmsg")作为TextBox;
OdbcCommand cmd =新的OdbcCommand();
-----------
cmd.CommandText =插入到message(Name,Email,Message)值(@ Name,@ Email,@ Message)";
--------------
cmd.Connection = objcon;
cmd.Parameters.AddWithValue("@ Name",Name.Text);
cmd.Parameters.AddWithValue("@ Email",Email.Text);
cmd.Parameters.AddWithValue("@ Message",Message.Text);
----------------
objcon.Open();
cmd.ExecuteNonQuery();
objcon.Close();
loadgridview();
}}}
-------------------------------
我尝试使用gridview插入值,但无法插入数据库
空白地插入.我一直保持Empid为增量.每次插入时均为增量
该字段的其余部分为空.
注意:通过断点
cmd.Parameters.AddWithValue("@ Name",Name.Text);
我在Name.Text中得到了值.文本为"anu"
但在插入qry中没有显示值,仅显示@name.
你能告诉我我在哪里犯错了吗?
感谢Adv
Anu

web.config
<appSettings>
<add key="prtest" value="DSN=MYSQL"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="DSN=Mysql;uid=root;pwd=mysql" providerName="System.Data.Odbc"></add>
</connectionStrings>
---------------
using System.Data.Odbc;
using System.Data;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
OdbcConnection objcon = new OdbcConnection(ConfigurationManager.AppSettings["prtest"]);

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadgridview();
} }
private void loadgridview()
{
String qry = " select * from message ";
OdbcDataAdapter da = new OdbcDataAdapter(qry, objcon);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "InsertRecord")
{
-------------
var Name = GridView1.FooterRow.FindControl("txtname") as TextBox;
var Email = GridView1.FooterRow.FindControl("txtemail") as TextBox;
var Message = GridView1.FooterRow.FindControl("txtmsg") as TextBox;
OdbcCommand cmd = new OdbcCommand();
-----------
cmd.CommandText = " insert into message(Name,Email,Message) values(@Name,@Email,@Message)";
--------------
cmd.Connection = objcon;
cmd.Parameters.AddWithValue("@Name", Name.Text);
cmd.Parameters.AddWithValue("@Email", Email.Text);
cmd.Parameters.AddWithValue("@Message", Message.Text);
----------------
objcon.Open();
cmd.ExecuteNonQuery();
objcon.Close();
loadgridview();
} }}
-------------------------------
i try to insert value by using gridview,but i cant able to insert into database
blankly its insert. i kept empid is increment .everytime i insert is increment
rest of the field is null.
Note:through breakpoint
cmd.Parameters.AddWithValue("@Name", Name.Text);
i got value in Name.Text has "anu"
but in insert qry no value is showing just @name is displaying.
can u tell me where i did mistake,
thanks in Adv
Anu

推荐答案

看看 http://support.microsoft. com/kb/310130 [ ^ ]

Have a look at http://support.microsoft.com/kb/310130[^]

Insert into message(Name,Email,Message) Values (?,?,?)



ODBC参数仅在位置而不是名称上起作用,因此只要它们按照在SQL中出现的顺序被创建并添加到Command对象的Parameters集合中,ODBC就能将正确的参数值与正确的标记匹配.

我还将在创建OdbcCommand对象之前打开连接

最好的问候
Espen Harlinn



ODBC parameters work on position, not name, so as long as they are created and added to the Parameters collection of the Command object in the order that they appear in the SQL, ODBC will be able to match the correct parameter value to the correct marker.

I would also open the connection before creating the OdbcCommand object

Best regards
Espen Harlinn


这是因为您在实际将值分配给那些参数之前已经编写了查询.

尚未分配值时如何具有这些值??

相反,请执行以下操作....

This is because you have written the query before you are actually assigning the values to those parameters.

How it will have the values when they are not assigned yet???

Instead do as follows....

cmd.CommandText = " insert into message(Name,Email,Message) values(''" + Name.Text + "'',''" + Email.Text + "'',''" + Message.Text + "'')";



无需使用以下三行...



There is no need to use these following three lines...

cmd.Parameters.AddWithValue("@Name", Name.Text);
cmd.Parameters.AddWithValue("@Email", Email.Text);
cmd.Parameters.AddWithValue("@Message", Message.Text);



让我知道它是否有效....



Let me know whether it works or not....


这篇关于没有错误,但是没有使用网格将数据插入到db(mysql)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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