|
MAW30 wrote: it only works when the button is in the same class Also, PerformClick() won't work if the button is disabled.
/ravi
|
|
|
|
|
why i cant store vales in database ???
private void Form8registration_Load(object sender, EventArgs e)
{
conn.Open();
}
private void ExecuteInsert(string empname, string desgination, string emailid, string phone, string skype, string address, string shifttime, string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (conn)
{
try
{
SqlCommand cmdd = new SqlCommand();
//cmdd.CommandType = CommandType.StoredProcedure;
cmdd.CommandText = "Insert into registrationn values (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
// cmdd.Parameters.Add("@empid", SqlDbType.VarChar, 150).Value = textBox7_empid.Text;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
cmdd.Dispose();
}
catch (Exception ex)
{
string msg = "insert error ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}
private void button2_reset_Click(object sender, EventArgs e)
{
Reset();
}
private void Reset()
{
textBox1_add.Text = "";
textBox3_phoneno.Text = "";
textBox4_emailid.Text = "";
textBox5_desgination.Text = "";
textBox6_name.Text = "";
textBox7_empid.Text = "";
comboBox1_shifttime.SelectedText= "";
textBox1_skpid.Text = "";
}
private void button3_cancel_Click(object sender, EventArgs e)
{
Close();
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text, textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
1 - Use Pre Tag To format your code, and make it readable.
2- In The Catch use
MessageBox.Show ( ex.Message)
So we can know what kind of error you get.
2 - Show me the Your Table Structure and Your Stored Procedure Code.
private void Form8registration_Load(object sender, EventArgs e)
{
conn.Open();
}
private void ExecuteInsert(string empname, string desgination, string emailid, string phone, string skype, string address, string shifttime, string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (conn)
{
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "Insert into registrationn values (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
cmdd.Dispose();
}
catch (Exception ex)
{
string msg = "insert error ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}
private void button2_reset_Click(object sender, EventArgs e)
{
Reset();
}
private void Reset()
{
textBox1_add.Text = "";
textBox3_phoneno.Text = "";
textBox4_emailid.Text = "";
textBox5_desgination.Text = "";
textBox6_name.Text = "";
textBox7_empid.Text = "";
comboBox1_shifttime.SelectedText= "";
textBox1_skpid.Text = "";
}
private void button3_cancel_Click(object sender, EventArgs e)
{
Close();
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text, textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
There is no stored procedure
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
and my stored procedure for the registration form is
ALTER PROCEDURE registrtn
(
@empname varchar(150),
@desgination varchar(150),
@emailid varchar(150),
@phone varchar(150),
@skype varchar(150),
@address varchar(150),
@shifttime varchar(150),
@panel varchar(150)
)
As
begin
INSERT INTO registrationn VALUES(@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)
select SCOPE_IDENTITY() as empid
end
|
|
|
|
|
..your command is filled with a sql-statement, and set to text. Also note that the using-statement would dispose your connection-object.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
this is my code can u tell where is the error so i can change ????
private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel) VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
cmdd.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text , textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
Can you explain me why you are using the using directive, and what it actually does? And how are you testing the code, from the IDE (pressing F5) or standalone?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
yes am testing my code by pressing F5
and my stored procedure for the registration form is
ALTER PROCEDURE registrtn
(
@empname varchar(150),
@desgination varchar(150),
@emailid varchar(150),
@phone varchar(150),
@skype varchar(150),
@address varchar(150),
@shifttime varchar(150),
@panel varchar(150)
)
As
begin
INSERT INTO registrationn VALUES(@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)
select SCOPE_IDENTITY() as empid
end
am using using directive because when i run the program that time it may showing the error object reference not in instance of object, by using using this i can implement the structured in proper way
|
|
|
|
|
Right, the problem seems fairly straightforward. Basically, you are attempting to do an insert into the registration table without specifying the columns that will be inserted into. Now, the fact that you have a select in there for the scope_identity tells me that you have an identity column on that table - in other words, you have a column mismatch between what's in the table, and what you are inserting. To resolve this, all you need to do is explicitly define the columns you are inserting into in the insert statement.
|
|
|
|
|
YES i put identity column in my registration table and i also mentioned the column name in the insert query but the data is not displayed in the database when i executing the stored procedure and put the values then its inserted but when i put the value at the run time winform then it does not go to the database so tell me how can i do ???
|
|
|
|
|
see this i also mentioned the column name in insert query but data not display in the database
private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel) VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 150).Value = phone ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text.Trim() , textBox5_desgination.Text.Trim(), textBox4_emailid.Text.Trim(), textBox3_phoneno.Text.Trim(), textBox1_skpid.Text.Trim(), textBox1_add.Text.Trim(), comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
}
else
{
MessageBox.Show("data not stored ");
}
}
}
}
|
|
|
|
|
And what happens when you step over the code? Do you get an exception? Please, provide some more details - remember that we can't see what's on your computer screen, so you have to help us. Plus, is your data going into SQL Server or Sql CE? This makes a big difference - and how do you know the data hasn't inserted?
|
|
|
|
|
yes i passed message using exception in catch and now i get the error object reference not instance of object ? i will [provide you everything i cant check my data is going to sql server or sqlce because i dont know and the data is not stored in the database when i executing the form using breakpoint that time it display in the text or mentioned fields in code but when i go to database there is no value shown
how can i check my entered value is going to SQL server or not ????
and my code of the application private void ExecuteInsert(string empname,string desgination,string emailid,string phone, string skype ,string address,string shifttime,string panel)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataReader rdr = null;
try
{
SqlCommand cmdd = new SqlCommand();
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel)" + " VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar , 150).Value = empname ;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar,150).Value = desgination ;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid ;
cmdd.Parameters.Add("@phone", SqlDbType.Int ).Value = Convert.ToString (phone) ;
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value =skype ;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address ;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = shifttime ;
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel ;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery ();
rdr = cmdd.ExecuteReader();
int jh;
int.TryParse(p1.Value.ToString(), out jh);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void button1_submit_Click(object sender, EventArgs e)
{
if (radioButton1_employe.Checked == true)
{
ExecuteInsert(textBox6_name.Text , textBox5_desgination.Text, textBox4_emailid.Text, textBox3_phoneno.Text, textBox1_skpid.Text, textBox1_add.Text, comboBox1_shifttime.SelectedItem.ToString(), radioButton1_employe.Checked.ToString());
////////here error showing object reference not an instance of object
}
else
{
MessageBox.Show("data not stored ");
}
}
|
|
|
|
|
Put a breakpoint on the line you have put in bold, and when the application breaks here, hover over each value in turn. See which one of those is giving you a null reference (which causes this error). BTW - you are attempting to execute a datareader in your code, and you have nothing to read into it.
|
|
|
|
|
sir when i put breakpoint on that line there is nowhere null show in any field and in every field have value that i had entered during the exucutime time of the program ...
|
|
|
|
|
HELLO SIR MY DATA NOT GOING TO SQL SERVER I DONT KNOW WHY MY CONNECTION STRING IS
<connectionstrings>
<add name="cn" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True"
="" providername="System.Data.SqlClient">
|
|
|
|
|
There's no need to shout - we help out voluntarily, when our schedule permits. You need to step over your code and inspect the variables that are in there - one of them is null. We can't tell which one - only you can do that as you are the one sat in front of your computer.
If you inspect the full exception (use the .ToString() method on the exception rather than just the Message), you should get an idea on which line is at fault - and you can use that to narrow down your debugging.
|
|
|
|
|
my program is executed properly but insert and delete query is not working . i dont know why this not working . i checked the data that is going on the SQLServer but can not inserted in the database so tell my why its not inserted in the database
my inserted query is
cmdd.CommandText = "INSERT INTO registrationn(empname,desgination,emailid,phone,skype,address,shifttime,panel)" + " VALUES (@empname,@desgination,@emailid,@phone,@skype,@address,@shifttime,@panel)";
cmdd.CommandType = CommandType.Text;
cmdd.Connection = conn;
cmdd.Parameters.Add("@empname", SqlDbType.VarChar, 150).Value = empname;
cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 150).Value = desgination;
cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 150).Value = emailid;
cmdd.Parameters.Add("@phone", SqlDbType.Int).Value = Convert.ToString(phone);
cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 150).Value = skype;
cmdd.Parameters.Add("@address", SqlDbType.VarChar, 150).Value = address;
cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 150).Value = Convert.ToString (shifttime);
cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 150).Value = panel;
//SqlParameter p1 = new SqlParameter("@ret", SqlDbType.VarChar, 150);
//p1.Direction = ParameterDirection.ReturnValue;
cmdd.ExecuteNonQuery();
|
|
|
|
|
Is this a file based SQL Server? Is it part of your solution? If it is, check to see what the properties are that are on your file - if it's set to Copy Always, then it will overwrite the file that is in your Debug or Release directory whenever you build it (and make sure you look at the one that is in that directory rather than the one that is to be copied over).
|
|
|
|
|
sir can u help me if i send u file can u make them in correct way ???? because am in trouble and in my database everything is working but data is not inserted and deleted ...please sir its my request to you for u its just 5 min
|
|
|
|
|
Normally, I'd say no, but if you want to upload it to DropBox and put the link here, I'll d/l it and have a look.
|
|
|
|
|
|
You'll have to make them public. They won't open for me.
|
|
|
|
|
I'll need to see the whole project as well - including the database.
|
|
|
|
|