data not inserting in tables.

brijendra

New member
Joined
Sep 22, 2016
Messages
2
Programming Experience
Beginner
hello sir / madam,

i am new to c#.net and i like to program,i have a table named login which have id,username and password columns, i also have a login form to send data in Login table i want to store input data in table but i am receiving the error - " An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll". what am i doing wrong? i am using visual studio 2015.please help! thank you.here is my code -


[FONT=&quot]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Log.mdf;Integrated Security=True;Connect Timeout=30;");
SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From Login where username ='" + userNametextBox.Text + "'and password='" + PasswordtextBox.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{

this.Hide();
Main ss = new Main();
ss.Show();

}
else
{
MessageBox.Show("Please check your username and password ");
}

}

private void btnInsert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Log.mdf;Integrated Security=True;Connect Timeout=30;");
SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From Login where username ='" + userNametextBox.Text + "'and password='" + PasswordtextBox.Text + "'", con);

SqlCommandBuilder cmb = new SqlCommandBuilder(sda);
//DataTable dt = new DataTable();
DataSet ds = new DataSet();
sda.Fill(ds, "Login");
ds.Tables[0].Constraints.Add("Id", ds.Tables[0].Columns[0], true);
DataRow row;
row = ds.Tables[0].NewRow();
row["username"] = userNametextBox.Text;
row["password"] = PasswordtextBox.Text;
ds.Tables[0].Rows.Add(row);
sda.Update(ds.Tables[0]);
MessageBox.Show("record added", this.Text);

}

private void userNametextBox_TextChanged(object sender, EventArgs e)
{

}
}
}
[/FONT]
 
Where EXACTLY in your code does that exception get thrown? What is the actual error message? This information is provided to you to help diagnose the issue. You should try to use it for yourself first but, if you can't, at least pass it on to us so that we don't have to guess. The error message will probably tell you exactly which argument is the issue and know the line of code and the argument is a large part of the battle won.
 
Please see attatched image for error

Where EXACTLY in your code does that exception get thrown? What is the actual error message? This information is provided to you to help diagnose the issue. You should try to use it for yourself first but, if you can't, at least pass it on to us so that we don't have to guess. The error message will probably tell you exactly which argument is the issue and know the line of code and the argument is a large part of the battle won.


Thank you for your kind reply. i have added error image, kindly review.
 

Attachments

  • error image.gif
    error image.gif
    133.3 KB · Views: 64
So, the error message states clearly:
Column 'username' does not belong to table Login.
The issue seems rather obvious to me. I don't want to discourage you from posting here when you have genuine issues but I would encourage you to use the information at your disposal first. Don't ignore information that the IDE gives you. It's now taken six hours for you to find out from me what you should have learned immediately when the exception was thrown.
 
Back
Top Bottom