hello,
i'm just about to start C#.net using PostgreSQL as my database... i used Npgsql as my connector.. i can access my dbase using it and i can display the data to grid. but i need help for inserting into dbase. here's my code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Npgsql;// <-- must be added through reference then browse the .dll, inorder to use connection,command,reader etc.
using System.Windows.Forms; // <-- also added reference under .NET tab then "System.Windows.Forms", inorder to use msgbox
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string DefCon = "server=localhost;database=testing;uid=postgres;pwd=argets";
NpgsqlConnection MyConnection = new NpgsqlConnection(DefCon);
string STRsql = " SELECT * FROM nametable where last ='" + TextBox1.Text + "' and first like '" + TextBox2.Text + "'";
NpgsqlCommand MyCommand = MyConnection.CreateCommand();
MyCommand.CommandText = STRsql;
NpgsqlDataAdapter myDataAdapter = new NpgsqlDataAdapter();
myDataAdapter.SelectCommand = MyCommand;
DataSet myDataSet = new DataSet();
MyConnection.Open();
int recexist = myDataAdapter.Fill(myDataSet);
if (recexist == 0)
{
MessageBox.Show("invalid username");
Response.Redirect("Default.aspx?Login=Invalid");
}
else
{
MessageBox.Show("welcome");
Response.Redirect("Default.aspx");
}
MyConnection.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
//**********************************
// hard coded to connect datagrid *
//**********************************
string DefCon = "server=localhost;database=testing;uid=postgres;pwd=argets";
NpgsqlConnection MyConnection = new NpgsqlConnection(DefCon);
string tmpSQL = "select * from nametable order by last asc";
NpgsqlCommand rsCommand = MyConnection.CreateCommand();
rsCommand.CommandText = tmpSQL;
NpgsqlDataAdapter rsDATAadapter = new NpgsqlDataAdapter();
rsDATAadapter.SelectCommand = rsCommand;
DataSet myDataSet = new DataSet();
MyConnection.Open();
rsDATAadapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
}
protected void bttnSave_Click(object sender, EventArgs e)
{
string DefCon = "server=localhost;database=testing;uid=postgres;pwd=argets";
NpgsqlConnection MyCon = new NpgsqlConnection(DefCon);
string Temp = "INSERT INTO nametable(last,first) VALUES('" + txtlast.Text + "','" + txtfirst.Text + "')";
NpgsqlCommand RsBem = MyCon.CreateCommand();
RsBem.CommandText = Temp;
NpgsqlDataAdapter RsDT = new NpgsqlDataAdapter();
RsDT.SelectCommand = RsBem;
MyCon.Open();
Response.Redirect("Default.aspx");
}
}
Please help me on bttnSave click. PM me if you reply to this topic..