|
|
|
สอบถามเรื่องการ Insert ค่าลงดาต้าเบสของ mysql หน่อยครับ มันบอกว่า colum userlogin ว่างอ่ะครับแก้ไขยังครับ |
|
|
|
|
|
|
|
พอกดบันทึกแล้วก็ error
Code
Column 'userlogin' cannot be null
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using MySql.Data.MySqlClient;
public partial class admin_add_Member : System.Web.UI.Page
{
MySqlConnection ObjCon;
protected void Page_Load(object sender, EventArgs e)
{
btnSave.Attributes.Add("Onclick", "return ChkData();");
String StringCon;
StringCon = ConfigurationManager.ConnectionStrings["ConectDB"].ConnectionString;
ObjCon = new MySqlConnection(StringCon);
ObjCon.Open();
}
protected void btnSave_Click(object sender, EventArgs e)
{
String StrInsert;
StrInsert = "INSERT INTO member(id_mem,userlogin,pass,name,isactive,datesave) VALUES (0,@userlogin,@pass,@name,@isactive,@datesave) ";
MySqlCommand SqlInsert = new MySqlCommand(StrInsert,ObjCon);
SqlInsert.CommandText = StrInsert;
SqlInsert.Parameters.Add("@userlogin", username.Text.Trim());
SqlInsert.Parameters.Add("@pass", password.Text.Trim());
SqlInsert.Parameters.Add("@name", name.Text.Trim());
if (isactive.Checked)
{
SqlInsert.Parameters.Add("@isactive", "Y");
}
else
{
SqlInsert.Parameters.Add("@isactive", "N");
}
SqlInsert.Parameters.Add("@datesave", "curdate()");
//try
//{
SqlInsert.ExecuteNonQuery();
//ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Msg", "alert('บันทึกข้อมูลเรียบร้อย');", true);
//}catch(Exception ex){
// Response.Write(ex.Message);
//}
}
}
Tag : ASP.NET, MySQL, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2012-03-18 21:08:21 |
By :
sleepington |
View :
1098 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูตัวอย่างนี้ครับ
Code (C#)
strSQL = "UPDATE files SET FilesName = ?sFilesName , FilesType = ?sFilesType WHERE FilesID = ?sFilesID ";
objCmd = new MySqlCommand(strSQL, objConn);
objCmd.Parameters.Add("?sFilesName", MySqlDbType.Binary).Value = imbByte;
objCmd.Parameters.Add("?sFilesType", MySqlDbType.VarChar).Value = strMIME;
objCmd.Parameters.Add("?sFilesID", MySqlDbType.Int32).Value = Request.QueryString["FilesID"];
objCmd.ExecuteNonQuery();
|
|
|
|
|
Date :
2012-03-18 21:20:37 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหมือนเดิมครับพี่ วิน code ที่ผมแก้นะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using MySql.Data.MySqlClient;
public partial class admin_add_Member : System.Web.UI.Page
{
MySqlConnection ObjCon;
protected void Page_Load(object sender, EventArgs e)
{
btnSave.Attributes.Add("Onclick", "return ChkData();");
String StringCon;
StringCon = ConfigurationManager.ConnectionStrings["ConectDB"].ConnectionString;
ObjCon = new MySqlConnection(StringCon);
ObjCon.Open();
}
protected void btnSave_Click(object sender, EventArgs e)
{
//StrInsert += " VALUES (NULL,'" + username.Text.Trim() + "','" + password.Text.Trim() + "','" + name.Text.Trim() + "','Y',curdate()) ";
// Response.Write(StrInsert);
String StrInsert;
StrInsert = "INSERT INTO member(id_mem,userlogin,pass,name,isactive,datesave)";
StrInsert += "VALUES(@id_mem,@userlogin,@pass,@name,@isactive,curdate())";
MySqlCommand SqlInsert = new MySqlCommand(StrInsert, ObjCon);
SqlInsert.CommandText = StrInsert;
SqlInsert.Parameters.Add("@id_mem", MySqlDbType.Int32).Value=null;
SqlInsert.Parameters.Add("@userlogin",MySqlDbType.VarChar).Value =username.Text.Trim();
SqlInsert.Parameters.Add("@pass", MySqlDbType.VarChar).Value = password.Text.Trim();
SqlInsert.Parameters.Add("@name", MySqlDbType.VarChar).Value = name.Text.Trim();
if (isactive.Checked)
{
SqlInsert.Parameters.Add("@isactive",MySqlDbType.Char).Value="Y";
}
else
{
SqlInsert.Parameters.Add("@isactive", MySqlDbType.Char).Value = "N";
}
SqlInsert.ExecuteNonQuery();
}
}
|
|
|
|
|
Date :
2012-03-18 21:39:45 |
By :
sleepington |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าสมมติเรา Insert แบบเถื่อนๆจะมีปัญหาไหมครับ
|
|
|
|
|
Date :
2012-03-18 21:46:02 |
By :
sleepington |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองแบบที่ผมให้ดูก่อนครับ
ใช้แบบปกติก้ได้ครับ แต่แบบ Patameters จะดีกว่าครับ
|
|
|
|
|
Date :
2012-03-18 21:54:43 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พี่วินครับลอง add Parameter แล้วนะครับแต่ไม่ได้เหมือนเดิม มันจะมีปัญหาตอนนี้ผมทำ key เป็น auto หรือเปล่าครับ
|
|
|
|
|
Date :
2012-03-18 22:21:30 |
By :
sleepington |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|