Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I WANT TO UPDATE MY TABLE WERE I WANT THE TIME TO INSERT INTO A SPECIFIC ROW
PLEASE HELP ME

here is my code hahaha PS. IM A BEGINNER at C#
C#
label1.Text = DateTime.Now.ToLongTimeString();

            con.Open();

            SqlCommand cmd = new SqlCommand("Updat", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@mode2", "Update");
            cmd.Parameters.AddWithValue("@Timein", label1.Text);
            cmd.Parameters.AddWithValue("@Username", username);
            cmd.ExecuteNonQuery();
            MessageBox.Show("TIMED IN");
       
            con.Close();



HERE IS MY CODE IN STORED PROCEDURE:
SQL
CREATE PROCEDURE [dbo].[Updat]
	@mode2 nvarchar(10),
	@Timein varchar(50),
	@Username nvarchar(50)
As
If @mode2 = 'Update'
BEGIN
UPDATE Login1
SET
Username = @Username,
Timein = @Timein
END


What I have tried:

i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
i don't know what to doooo
Posted
Updated 10-Jan-23 1:20am
v3
Comments
Richard Deeming 23-Oct-18 9:57am    
UPDATE Login1 SET Username = @Username, Timein = @Timein

That query is going to update EVERY record in the table to have the same Username and Timein.

Hopefully, you've added a unique constraint on the Username field, which would cause the update to fail. If not, you'll have destroyed your data, and will need to restore from a backup.

If you want to update a specific record, you need a WHERE clause:
UPDATE Login1 SET Timein = @Timein WHERE Username = @Username

Quote:
i don't know what to doooo

Read the error message and your code!
You are told that 'Procedure or function 'updat' expects parameter '@username', which was not supplied.'
which means that
SQL
CREATE PROCEDURE [dbo].[Updat]
@mode2 nvarchar(10),
@Timein varchar(50),
@Username nvarchar(50) -- THIS PARAMETER here
As
If @mode2 = 'Update'
BEGIN
UPDATE Login1
SET
Username = @Username, -- THIS PARAMETER here
Timein = @Timein
END

have no value because it is not provided in calling code.

We can't do anything because we don't know what you want to do.
make sure the procedure is not called from another place where the parameter is missing.
 
Share this answer
 
v3
Comments
Member 14026426 20-Oct-18 10:28am    
I want to insert the actual time into a specific User, but I don't know how can you help me?
Use the debugger, and you will probably find that right here:
C#
cmd.Parameters.AddWithValue("@Username", username);
The username variable contains null.

So now, you have to find out why - so use the debugger and track back through your code to find out where you expect a value to be put in it.
What value is that? Where does that come from? Why doesn't that have a value?

This needs the app to be running, so you can use the debugger to find this out - we can't do that for you because we can't run your code!
 
Share this answer
 
Comments
Member 14026426 20-Oct-18 10:30am    
I want to insert the actual time into a specific User, but I don't know how can you help me?
OriginalGriff 20-Oct-18 10:37am    
Sorry, but we can't - the problem needs to be looked at while your code is running, and you are using your data: neither of which we have any access to!

YOu need to use the debugger and look at exactly what is happening which ie is happening - you do know how to use the debugger, yes?
The existing answers give you pretty clear idea of what could be possibly the reason for the issue you are facing. Investigate it first. [check the @username parameter]

Another possibility:

Your code actually looks fine (if there isn't any issue with @username)

Since you say you are a beginner, I suspect there may be chances that you could have overlooked the build or configurations.

- Check if you have connected to the right database (this will be in your connection string in web.config)

- Check if you have built the project right and running the right version.

I would suggest you use debugger to dig into the error. It will help you locate the root-cause of the issue and eventually, solve it.
 
Share this answer
 
Comments
DhineshDK 4-Aug-21 8:58am    
SqlCommand cmd = new SqlCommand("UserDetail", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UserDetail";
//cmd.Parameters.Add(new SqlParameter("@UserId", txtUserId.Text));
cmd.Parameters.Add(new SqlParameter("@UserName", txtUserName.Text));
cmd.Parameters.Add(new SqlParameter("@PassWord", txtPassWord.Text));
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
this is not working
DhineshDK 4-Aug-21 8:59am    
it shows
Procedure or function 'UserDetail' expects parameter '@UserId', which was not supplied.
DhineshDK 4-Aug-21 8:59am    
create table Userdetails
(
UserId int IDENTITY (1,1) PRIMARY KEY
,UserName varchar(20)
,PassWord varchar(20))
drop table Userdetails;
DhineshDK 4-Aug-21 8:59am    
create PROCEDURE UserDetail(
@UserId int
,@UserName varchar(20)
,@PassWord varchar(20))
AS
BEGIN
insert into Userdetails (UserName,PassWord) values(@UserName,@PassWord)
END

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900